Http.post 对服务器的请求不会随即发送正文。在服务器端,请求体是空的。我在下面添加了代码

Http.post request to server doesn't send the body with it in flutter. On the server side, the body of request is empty. I have added the code below

Future<User> authUser(String username, String password) async (
Map data = {
'username' : username,
'password' : password,
};
var authUserResp = await http.post (
new Uri.http('localhost:8000','apiquery'),
headers: {
'Content-type' : 'application/json',
'Accept' : 'application/json',
},
body: json.encode(data),
);
print(authUserResp.statusCode);
return User.fromJson(jsonDecode(authUserResp.body)['data']);
)

在 Postman 中成功 post 操作的状态代码是 201,表示已创建。但是在 flutter 中,返回的状态代码是 200。在本地 api 服务器上,请求的主体是一个空映射,如 {}。

函数中的参数是'password'而不是'Password correct that

data 的定义不是 Map

尝试更正地图,看看是否有帮助:

    Map data = {
      'username': username,
      'password': password,
    };

您的函数无效。

这是代码。我只是做了一些改动。

      Future<Usert> authUser(String username, String password) async {
Map<String, dynamic> data = {
  'username': username,
  'password': password,
};
var authUserResp = await http.post(
  new Uri.http('localhost:8000', 'apiquery'),
  headers: {
    'Content-type': 'application/json',
    'Accept': 'application/json',
  },
  body: json.encode(data),
);
print(authUserResp.statusCode);
return User.fromJson(jsonDecode(authUserResp.body)['data']);

}