Api授权在邮递员工作,但不在flutter工作



我创建了一个带有授权的api,它在poster中正常工作,但我无法在flutter中使用授权,我在flutter上得到了Statuscode:401(未授权(,但在poster上得到了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());
headers: <String, String>{
'Content-Type': 'application/json', //add context-type
'Accept': 'application/json',//add accept
'Authorization': 'Bearer $token',
}

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

最新更新