Send custom headers

Attach arbitrary custom headers to a request — useful for request-tracing IDs, API versions, language preferences, and more.

Overview

Attach arbitrary custom headers to a request — useful for request-tracing IDs, API versions, language preferences, and more.

In code

curl https://api.example.com/data \
  -H 'X-Request-Id: 7f3c-abc' \
  -H 'Accept-Language: ko-KR' \
  -H 'User-Agent: my-app/1.2.0'
await fetch('https://api.example.com/data', {
  headers: {
    'X-Request-Id': '7f3c-abc',
    'Accept-Language': 'ko-KR',
    'X-Api-Version': '2024-01'
  }
});
import requests

res = requests.get('https://api.example.com/data', headers={
    'X-Request-Id': '7f3c-abc',
    'Accept-Language': 'ko-KR',
    'X-Api-Version': '2024-01',
})

Details

Header names are case-insensitive. By convention, standard headers use Camel-Dash casing (`Accept-Language`); non-standard extension headers historically used an `X-` prefix (deprecated by RFC 6648 but still common). A correlation ID like `X-Request-Id` is invaluable for debugging distributed systems.

In curl, repeat `-H` for each header. `fetch` and `requests` take an object/dict all at once. If you must send the same header name multiple times, check the browser `Headers.append` API or the way `requests` handles lists.

In the browser you cannot set forbidden headers such as `Host`, `Content-Length`, or `Origin` from script. Also, adding custom headers can push a request out of the 'simple' category and trigger a CORS preflight (OPTIONS), so review the server's CORS configuration too.

Related headers

Related status codes