Add HTTP headers to request
You can add your custom headers to a request.
Headers are passed as JSON in headers
parameter.
tip
Remember to encode this parameter like in the example below.
Example
In the example below, we add two headers:
Date: Mon, 12 Dec 2022 07:28:00 GMT
,X-Custom-Header: value
.
These headers will be forwarded to the requested page.
- cURL
- Python
- JavaScript
curl -G \
--data-urlencode 'url=https://httpbin.org/headers' \
--data-urlencode 'headers={"date": "Mon, 12 Dec 2022 07:28:00 GMT", "x-custom-header": "value"}' \
'https://scraping.narf.ai/api/v1/?api_key=[your API key]'
import requests
import json
from urllib.parse import quote_plus
url = quote_plus("https://httpbin.org/headers")
api_key = "[your API key]"
headers = quote_plus(
json.dumps({
"date": "Mon, 12 Dec 2022 07:28:00 GMT",
"x-custom-header": "value"
})
)
print(requests.get(f"https://scraping.narf.ai/api/v1/?api_key={api_key}&url={url}&headers={headers}").content)
import axios from "axios";
const url = encodeURIComponent("https://httpbin.org/headers");
const apiKey = "[your API key]";
const headers = encodeURIComponent(
JSON.Stringify({
"date": "Mon, 12 Dec 2022 07:28:00 GMT",
"x-custom-header": "value",
})
);
const response = await axios.get(
`https://scraping.narf.ai/api/v1/?api_key=${apiKey}&url=${url}&headers=${headers}`
);
console.log(response.data);
In the response from https://httpbin.org, you will be able to validate that the desired headers were indeed included in the request:
{
"headers": {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en-US",
"Date": "Mon, 12 Dec 2022 07:28:00 GMT",
"Host": "httpbin.org",
"Sec-Ch-Ua": "\"Google Chrome\";v=\"107\", \"Chromium\";v=\"107\", \";Not A Brand\";v=\"99\"",
"Sec-Ch-Ua-Mobile": "?0",
"Sec-Ch-Ua-Platform": "\"Windows\"",
"Sec-Fetch-Dest": "document",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-Site": "none",
"Sec-Fetch-User": "?1",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36",
"X-Amzn-Trace-Id": "Root=1-63963404-57e4efe21185436d73ea000e",
"X-Custom-Header": "value"
}
}