When using curl for network communication, let’s briefly learn about how to set headers.
Using the Header Option with curl Command
To use header values in curl commands, set the header values after the -H or --header option as follows:
curl --header or -H "header option: header value"The method is very simple as above. After the header option you want to use, pass the value together using a colon (:).
? What values can be sent with the header option?
Typically, the header option is used to transmit values like the following:
- User-Agent // Information on the client (browser or application)
- Authorization // Token and credentials information for authentication
- Content-Type // MIME type information of the data being transmitted
- Accept // MIME type information of the content that the client can receive
- X-Requested-With // Information to identify AJAX requests
In addition to these, other standard HTTP headers like Cache-Control, Connection, Accept-Encoding, etc. can also be included.
Examples of Sending Headers with curl
Let’s make a few examples to understand in more detail.
@ Sending Multiple Header InformationWhen sending more than one piece of header information, use -H repeatedly.
$ curl -H "User-Agent: MyApp/1.0" -H "Authorization: Bearer YOUR_ACCESS_TOKEN" http://example.com
@ Sending Accept Type Information$ curl -H "Accept: application/json" http://example.com
@ Sending User-Agent Value$ curl -H "User-Agent: MyApp/1.0" http://example.com
So far, we’ve briefly learned how to use curl.