我正在尝试使用以下代码进行身份验证,但无法成功。看来json结构不正确。
我尝试使用kotlin在本机android上进行检查,并且可以正常工作。
下面是日志
flutter: {data: {email: nelloresec@xxx.com, password: xxx}}
flutter: "{\"errors\":[]}"
flutter: http://xxx.xxx.xx
flutter: Instance of 'Response'
flutter: 400
flutter: {errors: []}
答案 0 :(得分:0)
类型为content-type
,String
或List<int>
的正文的默认Map<String, String>
为'text/plain'
,我想您想要'application/json'
>
此外,您可以直接将用户对象传递给body参数(因为toJson将由json.encode()
函数自动调用)。
最后,您不需要同时使用await
和.then(...)
,await
就足够了:
void userLogin(String url, User user) async {
http.Response response = await http.put(
'$url/api/login',
headers: {
HttpHeaders.contentTypeHeader: 'application/json',
},
body: json.encode(user),
);
if (response.statusCode == 200) {
print(url);
print(response.statusCode);
print(json.decode(response.body));
} else {
print(json.encode(response.body));
print(url);
print(response);
print(response.statusCode);
print(json.decode(response.body));
throw Exception("Error while fetching data");
}
}