getRequest function

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

GET request to fetch data from URL

Implementation

Future<String?> getRequest(
  Uri url, {
  Map<String, String> headers = const {},
  bool headless = false,
  bool debug = false,
  Uri? proxyUrl,
  bool cacheResponse = true,
}) async {
  printLog("HTTP GET: $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("GET URL: $url", debug, color: LogColor.yellow);
  try {
    var html = await http
        .get(
          url,
          headers: headers,
        )
        .timeout(
          Duration(seconds: 30),
        );
    String body = utf8.decode(html.bodyBytes);
    if (cacheResponse) {
      saveCacheLog(body, debug);
    }
    return body;
  } catch (e) {
    printLog(e.toString(), debug, color: LogColor.red);
    return null;
  }
}