Dart-Http获取正文请求 [英] Dart - Http Get request with body
本文介绍了Dart-Http获取正文请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用dart发送带有json正文的HTTP GET请求.我知道这是可能的,因为我过去做过,但是找不到文件/将其重新编码.诸如dart:http之类的软件包不允许与GET请求一起发送正文.
I want to send an HTTP GET request with json body using dart. I know this is possible, because I've done it in the past but can't find the files/recode it. Packages like dart:http doesn't allow to send a body along with an GET request.
感谢帮助
推荐答案
我不太确定问题应该在哪里,但是我为Dart VM制作了这个示例,我想它可以满足您的要求:
I am not really sure where the problem should be but I have made this example for Dart VM which I guess does what you want:
import 'dart:convert';
import 'dart:io';
Future<void> main(List arguments) async {
final response =
await getCallWithBody('http://localhost:8080', {"Key": "Value"});
response.forEach(print);
}
Future<List<String>> getCallWithBody(String address, Object object) async {
final client = HttpClient();
final request = await client.getUrl(Uri.parse(address));
request.contentLength = -1;
request.add(utf8.encode(json.encode(object)));
await request.flush();
return (await request.close())
.transform(utf8.decoder)
.transform(const LineSplitter())
.toList();
}
这篇关于Dart-Http获取正文请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文