postRequest function

Future<String?> postRequest(
  1. Uri url, {
  2. Object? body,
  3. Map<String, String> headers = const {},
  4. bool debug = false,
  5. Uri? proxyUrl,
  6. bool? cacheResponse,
})

POST request to fetch data from URL

Implementation

Future<String?> postRequest(
  Uri url, {
  Object? body,
  Map<String, String> headers = const {},
  bool debug = false,
  Uri? proxyUrl,
  bool? cacheResponse,
}) async {
  printLog("HTTP POST: $url", debug, color: LogColor.yellow);
  if (proxyUrl != null) {
    if (headers.isNotEmpty && proxyUrl.host.contains("scrapingbee")) {
      proxyUrl = addQueryParamToProxy(proxyUrl, "forward_headers_pure", "true");
      Map<String, String> newHeaders = {};
      headers.forEach((name, val) {
        newHeaders["Spb-$name"] = val;
      });
      headers = newHeaders;
    }
    printLog("Prepending proxy URL...", debug, color: LogColor.yellow);
    url = Uri.parse("$proxyUrl=${Uri.encodeComponent(url.toString())}");
  }
  printLog("POST URL: $url", debug, color: LogColor.yellow);
  try {
    var html = await http
        .post(
          url,
          body: body,
          headers: headers,
        )
        .timeout(
          Duration(seconds: 30),
        );
    return utf8.decode(html.bodyBytes);
  } catch (e) {
    return null;
  }
}