oneShotHttpPoster function
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);
}
}