React Native-FETCH-授权标头不起作用

时间:2019-09-28 07:49:37

标签: reactjs react-native header authorization fetch

我通过使用react-native开发的应用程序进行了服务器查询。我使用了访存API,结果发现所有带有授权标头的查询都不起作用。不需要服务器端的Authorization标头的POST和GET方法REQUESTS效果很好。服务器端的某些查询受授权保护,当我进行包括授权标头在内的此类查询时,总是会收到“ 401:unauthorized”错误。 但是这样的查询在POSTMAN中效果很好。这里的任何建议都会有很大帮助。

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 个答案:

答案 0 :(得分:0)

问题已解决。我们使用了Fetch API,并且fetch Api将所有标头转换为小写(例如:授权),服务器端期望使用大写的起始字母(例如:Authorization)。将服务器端代码更改为不区分大小写后,一切正常。