Flutter POST请求主体在服务器端为空

时间:2020-05-22 11:27:50

标签: flutter dart

var post = jsonEncode({
    "coach_id":id,
    "date":_value ,
    "user_id":userId ,
    "timeslots":duration ,
});

print(post);

var response = await http.post(url,headers:{"Content-Type":"application/json"}, body: post);


if (response.statusCode == 200) {


var responseJson = json.decode(response.body);
print(responseJson);

  return response;
} else {
  return null;
}
  } catch (exception) {

    print('exception---- $exception');
    return null;
  }

2 个答案:

答案 0 :(得分:2)

因此,我已经使用邮递员完成了请求,并且了解到服务器接受:chosenEndDate内容类型。

所以执行邮递员请求的正确方法是:

1

,正确的代码是:

chosenEndDate_unix
application/x-www-form-urlencoded

答案 1 :(得分:1)

您看到的输出看起来比JSON字符串更像一个Map。您可能想先尝试设置硬编码数据,只是为了检查服务器端一切是否正常。

Map<String, dynamic> data = {
    "coach_id":id,
    "date":_value ,
    "user_id":userId ,
    "timeslots":duration
  };


HttpClient httpClient = new HttpClient();
HttpClientRequest request = await httpClient.postUrl(Uri.parse(url));

request.headers.set('Accept', 'application/json');
request.headers.set('Content-type', 'application/json');
request.add(utf8.encode(json.encode(data))); 
// utf8 is optional but a good idea so as to handle the different chars

HttpClientResponse response = await request.close();

String serverResponse = await utf8.decoder.bind(response).join();
print(serverResponse);