在帖子调用中传递授权标头

时间:2019-10-09 08:40:18

标签: flutter dart

我必须传递标头数据,我的代码看起来像这样,并且在传递此数据后,它给了我解析标头问题的失败。可能是什么原因 ?

Map < String, String > userData = {
    "client_id": "value",
    "client_secret": "value",
    "grant_type": "value"
};
Map < String, String > headersMap = {
    'content-type': 'application/json',
    'authorization': 'Basic <token to be passed>'
};
var jsonBody = json.encode(userData);
final encoding = Encoding.getByName('utf-8');
http.post(uri, body: jsonBody, headers: headersMap).then((http.Response r) {
    print(r);
    if (r.statusCode == 200) {
        Scaffold.of(context).showSnackBar(new SnackBar(content: new Text("User Info Updated"), ));
    } else {
        print(r.statusCode);
        print(r.body);
    }
});

我什至已经使用了dio并传递了标头数据

Map < String, String > headers = new Map < String, String > ();
headers['Authorization'] = "Basic <token>";
headers['Content-Type'] = "application/json";

Options options = Options(
    headers: headers,
);

var httpHeaders = {
    'Authorization': "Basic <token>",
    'Content-Type': "application/json"
};

dio.options.headers = httpHeaders / headers;

response = await dio.post('/oauth/token',
    data: jsonBody, options: options, );

我也尝试了一些没有任何标题的Post方法调用(示例post请求),并且效果很好

我希望标题解析问题消失,并获得适当的响应

1 个答案:

答案 0 :(得分:0)

请尝试使用相同的撇号(caracter中的标记)。希望对您有帮助

 map['Authorization'] = 'Bearer ${user.accessToken}';

var head = {
          "Authorization": "Bearer ${accessToken} ",
          "content-type": "application/json"}