api授权在邮递员中工作,但不工作

时间:2020-11-12 11:50:30

标签: flutter dart flutter-dependencies

我创建了一个具有授权的api,它在邮递员中可以正常工作,但是我无法在flutter中使用授权,但是在flutter中却得到了Statuscode:401(未授权),但是在邮递员中却得到了statuscode:200(成功) )。这是我的代码:

sharedPreferences = await SharedPreferences.getInstance();
    String Authorization =  sharedPreferences.getString("token"); 
    var data = await http.get("http://10.148.7.58/Election/api/TblDatas",
        headers: <String, String>{'Authorization': '$Authorization',}
    );



 //Also tried these
    //headers: {'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': '$Authorization',}
    //headers: {HttpHeaders.contentTypeHeader: "application/json", HttpHeaders.authorizationHeader: "$Authorization"}
   
    print('status code is' + (data.statusCode).toString());

2 个答案:

答案 0 :(得分:0)

headers: <String, String>{
  'Content-Type': 'application/json', //add context-type
  'Accept': 'application/json',//add accept
  'Authorization': 'Bearer $token',
 }

答案 1 :(得分:0)

确保您的服务器接受小写标题名称 - 在这种情况下授权,因为 Dart 会将它们小写。这愚弄了一些不符合 RFC 标准的服务器