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/dataThis method is commonly used for authentication, such as passing a bearer token:
curl -H "Authorization: Bearer YOUR_API_TOKEN" https://api.example.com/secure-dataSending 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/submitModifying 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.comTo 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