cURL Option | Description | Usage Example |
---|---|---|
-H , --header | Pass custom header(s) to server | curl -H "Header: Value" http://example.com |
-A , --user-agent | Set the User-Agent string | curl -A "MyAgent" http://example.com |
-e , --referer | Set the Referer header | curl -e "http://referrer.com" http://example.com |
-b , --cookie | Send cookies from string or file | curl -b "name=value" http://example.com |
-d , --data | Send data in a POST request | curl -d "key=value" http://example.com |
Sending custom HTTP headers with cURL is essential for testing and debugging web applications. In this article, we’ll explore how to send HTTP headers using cURL, a powerful command-line tool for transferring data with URLs.
Understanding HTTP Headers
HTTP headers are key-value pairs sent in requests and responses between clients and servers. They convey meta-information about the request or response, such as content type, authorization credentials, and caching directives. Common HTTP headers include:
Header Name | Purpose |
---|---|
User-Agent | Identifies the client software |
Accept | Specifies acceptable content types |
Authorization | Contains credentials for authentication |
Content-Type | Indicates the media type of the resource |
Referer | The address of the previous web page |
Why Send Custom HTTP Headers with cURL?
Custom HTTP headers allow you to:
- Simulate requests from different browsers or devices.
- Test APIs that require specific headers.
- Debug issues by modifying headers.
- Control caching and content negotiation.
- Authenticate requests to protected resources.
Sending HTTP Headers with cURL
Using the -H
or --header
Option
To send a custom HTTP header with cURL, use the -H
or --header
option followed by the header:
curl -H "Header-Name: Header-Value" http://example.com
Multiple Headers
You can send multiple headers by specifying multiple -H
options:
curl -H "First-Header: Value1" -H "Second-Header: Value2" http://example.com
Replacing or Modifing Default Headers
cURL automatically sends certain headers like Host
and User-Agent
. To replace a default header, specify it with a new value. For example, to change the User-Agent
:
curl -H "User-Agent: MyCustomAgent" http://example.com
If you want to remove a header, set it to an empty string:
curl -H "Accept:" http://example.com
Examples of Sending HTTP Headers with cURL
Example: Setting User-Agent
The User-Agent
header identifies the client software. To set it:
curl -H "User-Agent: Mozilla/5.0" http://example.com
Alternatively, you can use the -A
option:
curl -A "Mozilla/5.0" http://example.com
Example: Adding Authorization Header
For APIs that require authentication, you might need to send an Authorization
header:
curl -H "Authorization: Bearer your_token_here" http://api.example.com
Example: Setting Content-Type for POST Requests
When sending data in a POST request, you may need to specify the Content-Type
header:
curl -d '{"key":"value"}' -H "Content-Type: application/json" http://example.com/api
Example: Using Custom Headers with Proxies
If you’re using a proxy to send out your requests, you can combine proxy options with custom headers:
curl -x http://proxyelite.info:8080 -H "Custom-Header: Value" http://example.com
Tips and Best Practices
- Enclose Headers in Quotes: Always enclose header values in quotes to prevent shell interpretation, especially when they contain spaces or special characters.
- Verbose Mode: Use verbose mode
-v
to see the headers sent and received. This is useful for debugging:
curl -v -H "Header: Value" http://example.com
- Sensitive Data: Be cautious when sending sensitive data in headers, especially when sharing command examples or logs.
- Default Headers: Remember that cURL send some default headers. Overwriting them can affect how the server responds.
Common Mistakes to Avoid
- Missing Quotes: Forgetting quotes around headers can lead to errors or unexpected behavior.
- Incorrect Header Syntax: Headers should be in the format
Header-Name: Value
. - Case Sensitivity: While header names are case-insensitive, it’s good practise to use standard capitalization.
Conclusion
Sending HTTP headers with cURL is straightforward and powerful. Whether you’re testing APIs or simulating different clients, mastering cURL’s header options enhances your ability to interact with web servers effectively.