How to Add and Send HTTP Headers with cURL
The syntax for Appending Headers to HTTP Requests
First, it is important you know that there is no limit to the number of headers you can add. While there are standard headers you should add as most web servers will request them, there are some custom ones that specific web servers do need and you can equally create and send them along. In other to send headers, you need to use the (— H) flag and then add the header as a key-value pair but in the form of a string. Below is an example of how it should look.
curl [URL] —H ‘Content-Type: application/json’
As you can see above, you start the command with the curl keyword, then the URL (you can replace it with an actual URL), then the HTTP header you want to send. In our case, the HTTP header above is the content type header, telling the server the data it sends is in the form of a JSON, typically of an API.
Send Multiple Headers with cURL
In the syntax I provided above, only one header is sent with the request. In reality, you will need to send more than a single header. How then do you get that done? Turns out that sending multiple headers is not any different from sending a single header. All you have to do is send in multiple —H and key-value pairs. Let's see how to do this in action below.
curl [URL] —H “Content-Type: application/json” —H “Accept: application/json”
Looking at the command above, you will notice I have added a new header known as the Accept header. What this header is telling its target web server is that it only accepts JSON responses. But that is not the point here. The point here is that it sends two headers, the content-type header and the accept header all in the same command.
Send the User-Agent Header
The user agent header is the header you send to identify your client software. In all web requests sent, user agents need to be sent. If you do not send the user agent, then your target server will know you are accessing their server using cURL as that will be the default user agent. For this reason, you must always modify the user agent header anytime you send web requests using cURL or any other HTTP request client.
The User Agent string is a special header and as such, there are multiple ways you can set it. Let’s take a look at the different ways you can configure user agents in curl.
curl —A “Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/81.0” [URL]
As you can see above, we use the —A flag to assign a Firefox user agent. With this, your target site will think you are visiting from a Firefox browser not knowing you are accessing via cURL. The method above is the default method because of the special status of this header. Let's see the other methods.
curl —user-agent “Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/81.0” [URL]
From the above, you see that instead of the —A flag, I used the —user-agent flag. The last method is using the —H as you would for other HTTP headers.
curl —H “User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/81.0” [URL]
Whether you use the —A, —user-agent, or —H flag, you will still achieve the same thing as they are all methods for setting the user agent string for cURL.
Send HTTP POST Requests with cURL
From the above examples, I sent the content type header which tells your target server that you send data of type application/json. However, I didn’t send any data to give you the full details of how it works. Let me show you how to get that done.
curl [URL] —H “Content-Type: application/json” —H “Accept: application/json” —d “[JSON DATA]”
As you can see above, it is the same command as the command for sending multiple headers at once. The only addition is the —d flag which carries the POST data. This should be in the form of a key-value pair and this is the JSON format.