httpPost function

Future<Response> httpPost(
  1. Uri uri, {
  2. Map<String, String>? headers,
  3. Object? body,
  4. bool enableDio = false,
})

The default http POST that support Logging

Implementation

Future<http.Response> httpPost(
  Uri uri, {
  Map<String, String>? headers,
  Object? body,
  bool enableDio = false,
}) async {
  final startTime = DateTime.now();
  initProxyClient(MultiSite.mainSiteUrl ?? uri);
  var url = HttpClientSetting.settingProxy(uri);

  if (enableDio) {
    try {
      final res = await Dio().post(url.toString(),
          options: Options(headers: headers, responseType: ResponseType.plain),
          data: body);
      printLog('🔼 POST:$url', startTime);

      final headersRes = <String, String>{};
      res.headers.forEach((name, values) {
        headersRes.addAll({name: values.join()});
      });

      final response = http.Response(
        res.toString(),
        res.statusCode!,
        headers: headersRes,
      );

      return response;
    } on DioException catch (e) {
      if (e.response != null) {
        final response =
            http.Response(e.response.toString(), e.response!.statusCode!);
        return response;
      } else {
        // ignore: only_throw_errors
        throw e.message ?? 'error';
      }
    }
  } else {
    var response =
        await _makeRequest(http.post(url, headers: headers, body: body));
    printLog('🔼 POST:$url', startTime);
    return response;
  }
}