How to Send Custom HTTP Headers With curl

Sending custom HTTP headers is a fundamental task when testing APIs or interacting with web services using the command-line tool curl. This article provides a quick, direct guide on how to use the -H (or --header) option in curl to include custom headers, send multiple headers, and modify default headers in your requests.

Sending a Single Custom Header

To add a custom header to a curl request, use the -H or --header option followed by the header name and value, separated by a colon, and enclosed in quotes.

curl -H "X-Custom-Header: MyValue" https://api.example.com/data

This method is commonly used for authentication, such as passing a bearer token:

curl -H "Authorization: Bearer YOUR_API_TOKEN" https://api.example.com/secure-data

Sending Multiple Custom Headers

If you need to send more than one custom header, pass the -H option multiple times within the same command.

curl -H "Content-Type: application/json" -H "X-API-Key: 12345" https://api.example.com/submit

Modifying or Removing Default Headers

Curl automatically includes several default headers in its requests, such as User-Agent and Accept. You can override these defaults by defining them with your own values:

curl -H "User-Agent: MyCustomUserAgent/1.0" https://example.com

To completely remove an automatic header from a request, specify the header name followed by a colon with no value:

curl -H "User-Agent:" https://example.com