Parameters as Headers

Learn how to scrape websites using header-based parameters with ScraperAPI in Java. Pass api_key, render, and instruction_set via headers to simplify requests.

Along with the “traditional” means of passing parameters, we also support passing parameters as headers. Passing parameters such as api_key, render, ultra_premium and instruction_set is very straightforward.

  • API REQUEST

Instead of including the parameters in the URL

import java.net.*;
import java.io.*;

public class Scrape {
  public static void main(String[] args) {

    try {
      String url = "https://api.scraperapi.com/?api_key=<YOUR_API_KEY>&url=https%3A%2F%2Fhttpbin.org%2Fip&render=true";
      URL urlForGetRequest = new URL(url);
      String readLine = null;
      HttpURLConnection conection = (HttpURLConnection) urlForGetRequest.openConnection();
      conection.setRequestMethod("GET");
      int responseCode = conection.getResponseCode();
      if (responseCode == HttpURLConnection.HTTP_OK) {
        BufferedReader in = new BufferedReader(new InputStreamReader(conection.getInputStream()));
        StringBuffer response = new StringBuffer();
        while ((readLine = in.readLine()) != null) {
          response.append(readLine);
        }
        in.close();
        System.out.println(response.toString());
      } else {
        throw new Exception("Error in API Call");
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}

you can just pass them as headers

Please note that the 'x-sapi-' prefix is used on each header to avoid collisions with headers used by target sites. We support all standard parameters available with the API. The instruction_set parameter is specifically supported only through headers.

  • PROXY MODE

Note that credentials must still be passed to the proxy in the manner shown above, not as headers.

Last updated

Was this helpful?