1

I made a server query via an app developed using react-native. I used fetch API and it turns out any query with authorization header not working. POST and GET method REQUESTS that don't expect Authorization headers at the server side works well. Some Queries at the server side are protected with authorization and when I make such queries including authorization header, I always get '401:unauthorized' error. But such queries works well with POSTMAN. Any suggestions here would be of great help.

getThingsApi() {

let uri = "https://example.com/things/";
let req = {
  method: "GET",
  credentials: "include",
  //mode: "no-cors",
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
    //'Access-Control-Request-Method': 'GET',
    //'Access-Control-Request-Headers': 'origin, x-requested-with',
    'Origin': '',
    'Host':'example.com',
    'authorization': 'Basic '+Base64.btoa('aaa:bbb'),
    'Cache-Control': 'no-cache'      
  }
};

fetch(uri, req)
  .then(response => response.json())
  .then(responseJson => {
  console.log("ResponseAxis::" + JSON.stringify(responseJson));
    console.log("ResponseAxis::" + JSON.stringify(responseJson));
    alert("ResponseAxis::" +JSON.stringify(responseJson));
  })
  .catch(error => {
    console.log("Error::" + JSON.stringify(error));
  });

}

3
  • Does your server expect/check CORS headers? In that case it is possible it rejects the empty 'Origin' header. Try removing it. Commented Sep 28, 2019 at 14:53
  • Still the same behaviour @tvanlaerhoven. If origin is the problem, it should'nt be returning any response for queries without Auth headers too, but thats not the case. API Queries without Auth headers works very well. Commented Sep 30, 2019 at 5:26
  • Issue is fixed. We used Fetch API and fetch Api converts all headers into lower-case(eg: authorization) and the server side expects upper-case starting letter(eg: Authorization). After changing the server side code to be case-insensitive, everything works fine. Commented Oct 9, 2019 at 9:11

1 Answer 1

1

Issue is fixed. We used Fetch API and fetch Api converts all headers into lower-case(eg: authorization) and the server side expects upper-case starting letter(eg: Authorization). After changing the server side code to be case-insensitive, everything works fine.

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

Comments

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.