Dart - Http Get request with body

人走茶凉 提交于 2021-02-08 11:15:51

问题


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.

thanks for help


回答1:


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();
}


来源:https://stackoverflow.com/questions/60157768/dart-http-get-request-with-body

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!