1

I am implementing an API which is based on POST method. Now, I have tried axios as well as fetch (promise) method but I am constantly having this error of "401 Authentication Failed".

  • For testing, I defined the data values to be sent into this.state as default values.
  • apiKey and userId are defined as var outside the React.Component.
  • Instead of using inbuilt btoa function, I am using the one from npm directory which is imported on the top.

Here is the React Component code:-

constructor(props) {
    super(props);

    this.state = {
      day: 1,
      month: 2,
      year: 2019,
      hours: 12,
      minutes: 59,
      tzone: 5.5,
      lat: 19.22,
      lon: 25.2
    };
  }

  componentDidMount() {

      const options = {
      headers: {
        "Authorization": "Basic" + btoa(userId+":"+apiKey),
        "Content-Type":'application/json'
      }
    };

      const url = `https://json.astrologyapi.com/v1/current_vdasha`
      const data = this.state

      fetch(url, { method: 'POST', 
        body: JSON.stringify(data), 
        headers: options })
        .then(res => res.json())
        .catch(error => console.error('error ---', error.message))
        .then(response => console.log('Success:', response));
  }

Any help is appreciated. Thank you.

2
  • We need to see the api code to understand why this fails. Commented Jan 31, 2020 at 6:19
  • @AthanasiosKataras What exactly should I provide you with? Here is what the API devs provide for reference -- var userId = ''; var apiKey = ''; var data = 'JSON Request Data'; var request = $.ajax({ url: "https://json.astrologyapi.com/v1/"+api, method: "POST", dataType:'json', headers: { "authorization": "Basic " + btoa(userId+":"+apiKey), "Content-Type":'application/json' }, data:JSON.stringify(data) }); // Returns A promiss return( request.then( function(resp){ return resp; }, function(err){ return err; })); } Commented Jan 31, 2020 at 6:39

2 Answers 2

2

In the header part of the request try adding space after "Basic" which should look like this : '''

const options = {
headers: {
    "Authorization": "Basic " + btoa(userId+":"+apiKey),
    "Content-Type":'application/json'
  }
};

''' As the authentication header first expects the type of authorization followed by space and then the api key

Sign up to request clarification or add additional context in comments.

1 Comment

I checked that out before also but it doesn't seem to fix the error.
0

When you pass headers to the fetch function, you pass it inside another object.

In the fetch function, try to change headers: options to headers: options.headers.

2 Comments

It seems like this was part of the fix but it produces 405 Method not allowed error.
POST json.astrologyapi.com/v1/current_vdasha 405 (Method Not Allowed)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.