我正在制作一个 flutter 的应用程序,并且我已经在Django中编写了一个服务器。当我向服务器发送 token 进行身份验证时,服务器会向我发送未定义 token 的错误。没有 token ,所有请求都可以正常工作,但是当我添加 token 时,它给了我一个错误
{detail: Authentication credentials were not provided.}
但是当我在modheader中添加 token 时,我的服务器工作正常
Authorization: Token bff0e7675d6d80bd692f1be811da63e4182e4a5f
这是我的 flutter 代码
const url = 'MY_API_URL';
var authorization = 'Token bff0e7675d6d80bd692f1be811da63e4182e4a5f';
final response = await http.get(
url,
headers: {
'Content-Type': 'application/json',
'Authorization': authorization,
}
);
final responseData = json.decode(response.body);
print('responseData');
print(responseData);
最佳答案
试试这个:
Map<String, String> headers = {
HttpHeaders.contentTypeHeader: 'application/json',
HttpHeaders.acceptHeader: 'application/json',
HttpHeaders.authorizationHeader: 'Token bff0e7675d6d80bd692f1be811da63e4182e4a5f'
};
并根据要求使用它们
final response = await http.get(
url,
headers: headers,
);
关于flutter - 如何在 flutter 朔迷离的请求中发送 token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60980239/