React native - FETCH - Authorization header not working

耗尽温柔 提交于 2021-02-11 16:47:26

问题


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));
  });

}


回答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.



来源:https://stackoverflow.com/questions/58144396/react-native-fetch-authorization-header-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!