0

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 Answers1

0

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();
}
julemand101
  • 28,470
  • 5
  • 52
  • 48