问题描述
我通过使用react-native开发的应用程序进行了服务器查询.我使用了访存API,结果发现所有带有授权标头的查询都不起作用.不需要服务器端的Authorization标头的POST和GET方法REQUESTS效果很好.服务器端的某些查询受授权保护,当我进行包括授权标头在内的此类查询时,总是会收到"401:unauthorized"错误.但是这样的查询在POSTMAN中效果很好.这里的任何建议都会有很大帮助.
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));
});
}
推荐答案
问题已解决.我们使用了Fetch API,并且fetch Api将所有标头都转换为小写(例如:授权),服务器端期望使用大写的起始字母(例如:Authorization).将服务器端代码更改为不区分大小写后,一切正常.
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.
这篇关于React Native-FETCH-授权标头不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!