Skip to content
All posts

How to send custom headers and cookies with a scrape

Getting started3 min read

Sometimes the target needs more than a URL: a specific User-Agent, an Accept-Language so it answers in the right language, an Authorization header for an API, or a cookie from a session you already have. This tutorial forwards custom headers and cookies through the API and proves they arrived, using httpbin.org, a test service that echoes back what it received.

Send custom headers

Put the headers you want the target to see in send_headers, an object of name to value. They are forwarded on the request to the target, in addition to what the API sends by default.

curl
curl https://scrape.land/v1/fetch \
  -H "X-Api-Key: YOUR_KEY" \
  -H "Content-Type: application/json" \
  -d '{"url": "https://httpbin.org/headers",
       "format": "text",
       "send_headers": {
         "User-Agent": "my-crawler/1.0 (+https://example.com/bot)",
         "Accept-Language": "de-DE",
         "X-Trace-Id": "tutorial-42"
       }}'

httpbin replies with the headers it saw. All three arrived as sent:

Terminal showing the send_headers request to httpbin.org/headers and the echoed headers, including User-Agent my-crawler/1.0, Accept-Language de-DE and X-Trace-Id tutorial-42
The real response, with httpbin's JSON parsed for readability.

Note the two headers in your curl command are different things. X-Api-Key and Content-Type go to scrape.land and are never forwarded. Only what is inside send_headers reaches the target. A few headers that control the connection itself (such as Host and Content-Length) are dropped, and so are the internal control headers the API uses to route your request, so a forwarded header can never change how the request is routed or billed.

Send cookies

cookies takes the value of a Cookie header, exactly as a browser would send it: name=value pairs separated by ; . Add "headers": true if you also want the target's response headers back, for example to read a Set-Cookie or a redirect Location.

curl
curl https://scrape.land/v1/fetch \
  -H "X-Api-Key: YOUR_KEY" \
  -H "Content-Type: application/json" \
  -d '{"url": "https://httpbin.org/cookies",
       "format": "text",
       "cookies": "session_id=abc123; currency=EUR",
       "headers": true}'
Terminal showing the cookies request to httpbin.org/cookies and a response where the target saw currency EUR and session_id abc123, plus a headers object with Content-Type and Server
Both cookies reached the target. The headers object holds the target's response headers (trimmed here to two).

The same thing in Python

Python
import requests

r = requests.post(
    "https://scrape.land/v1/fetch",
    headers={"X-Api-Key": "YOUR_KEY"},
    json={
        "url": "https://httpbin.org/cookies",
        "format": "text",
        "cookies": "session_id=abc123; currency=EUR",
        "send_headers": {"Accept-Language": "de-DE"},
        "headers": True,
    },
    timeout=60,
)
r.raise_for_status()
page = r.json()
print(page["status"], page["headers"].get("Content-Type"))
print(page["text"])

When to use which

A good citizen's User-Agent names your crawler and gives a contact URL, as in the example above. Only send cookies and credentials for accounts you are allowed to use.

What it costs

Headers, cookies and "headers": true are part of the fetch: each request above is 1 request unit. Adding "render": true makes it 5, or 1 with block_resources. See pricing.

Next steps

Every request option is listed in the docs. To interact with a page instead of sending a cookie, see fill a form and click a button. Create a free account and point the examples at httpbin to see your own headers.

Start free with 1,000 requests Read the docs