post static method

Future<String> post(
  1. Uri uri,
  2. Map<String, Object> headers,
  3. List<int> data
)

Implementation

static Future<String> post(
    Uri uri, Map<String, Object> headers, List<int> data) async {
  final client = HttpClient();
  try {
    const timeout = Duration(seconds: 15);
    final req = await client.postUrl(uri);
    headers.forEach((name, values) {
      req.headers.set(name, values);
    });
    req.contentLength = data.length;
    req.add(data);
    final res = await req.close().timeout(timeout);
    if (res.statusCode != HttpStatus.ok) {
      final body = await res.transform(utf8.decoder).join().timeout(timeout);
      throw Exception("request $uri error , status ${res.statusCode} $body");
    }
    final body = await res.transform(utf8.decoder).join().timeout(timeout);
    return body;
  } finally {
    client.close();
  }
}