oneShotHttpPoster function

Future<int> oneShotHttpPoster(
  1. Uri url,
  2. Map<String, String> headers,
  3. String body,
  4. Duration timeout,
)

One client per request (SendConfig.keepAlive = false).

Implementation

Future<int> oneShotHttpPoster(
  Uri url,
  Map<String, String> headers,
  String body,
  Duration timeout,
) async {
  final client = HttpClient()..connectionTimeout = timeout;
  try {
    final req = await client.postUrl(url).timeout(timeout);
    final bytes = utf8.encode(body);
    req.headers.contentType = ContentType.json;
    headers.forEach(req.headers.set);
    req.contentLength = bytes.length; // see KeepAliveHttpPoster: never chunked
    req.add(bytes);
    final res = await req.close().timeout(timeout);
    await res.drain<void>();
    return res.statusCode;
  } finally {
    client.close(force: true);
  }
}