Flutter Dart-如何使用HttpClient()发送发帖请求

时间:2019-10-05 11:28:09

标签: http flutter dart

我正在尝试使用HttpClient向服务器发送Post请求,但是我不确定在哪里实际设置需要发送的有效负载和标头。

    var client = new HttpClient();
    client.post(host, port, path);

client.post(host, port, path)仅具有3个参数,那么如何设置要发送的有效载荷?

预先感谢

2 个答案:

答案 0 :(得分:2)

post()使用POST方法打开HTTP连接并返回Future<HttpClientRequest>

所以您需要这样做:

final client = HttpClient();
final request = await client.post(host, port, path);
request.headers.set(HttpHeaders.contentTypeHeader, "plain/text"); // or headers.add()

final response = await request.close();

jsonplaceholder的示例:

final client = HttpClient();
final request = await client.postUrl(Uri.parse("https://jsonplaceholder.typicode.com/posts"));
request.headers.set(HttpHeaders.contentTypeHeader, "application/json; charset=UTF-8");
request.write('{"title": "Foo","body": "Bar", "userId": 99}');

final response = await request.close();

response.transform(utf8.decoder).listen((contents) {
  print(contents);
});

打印

{
  "title": "Foo",
  "body": "Bar",
  "userId": 99,
  "id": 101
}

或者您可以使用http库。

答案 1 :(得分:0)

SELECT a.NAME, a.VALUES, a.TAGS, s.VALUES_WANTED
FROM table_2 a


RIGHT JOIN table_1 s 
ON a.NAME = s.NAME 
AND a.VALUES = s.VALUES_WANTED