【发布时间】:2021-08-03 12:29:03
【问题描述】:
我正在尝试从我的 react-native 应用程序向 django api 发送一个获取请求,但我总是收到错误 {"detail":"Authentication credentials were not provided."}。
但是当我对邮递员尝试同样的请求时,一切正常,我尝试从邮递员本身获取代码并将其复制到我的 react-native 应用程序,但它同样出现未经授权的错误
这是从上述邮递员请求生成的代码,我直接复制并粘贴到我的 react-native 应用程序中,它会抛出上述未经授权的消息。
还尝试了 postman 为 curl 生成的代码,它可以工作,
请让我知道我在这里缺少什么
var myHeaders = new Headers();
//you can assume the token is correct
myHeaders.append("Authorization", "Token f38be1f33dbb5d6004108990a0a76");
var requestOptions = {
method: 'GET',
headers: myHeaders,
redirect: 'follow'
};
fetch("https://base_url/api/v1/endpoint", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
【问题讨论】:
-
还尝试了其他请求方法也不起作用
-
还尝试添加其他类型的标题以防万一
myHeaders.append("Accept", "*/*")myHeaders.append("Accept-Encoding", "gzip, deflate, br")但它们都没有帮助 -
你试过把内容类型设为application/json
-
和 'Accept':'application/json' 以防万一
-
是的,已经尝试过同样的结果
标签: django react-native api request fetch-api