Loading...

How to Add and Send HTTP Headers with cURL

Avatar
Chris Prosser

Are you looking for a how-to guide on how to send HTTP requests with headers using cURL? Then you are on the right page as the article below takes care of that in great detail for you.

cURL is quite versatile and used as a networking tool for transferring data without many developers even knowing. While this tool is used in more places than you will initially expect, not many developers know how to make use of it as there are more easy-to-use alternatives that will speed up your work and make things easier for you. However, if your task requires it, then learning how to use it is a must.

Sending HTTP requests is one of the core features of the cURL client. However, because of how misinformed some developers are, they do not know they can send HTTP headers and even fake it using cURL. This is the reason this article was written — to guide users on how to send HTTP headers with cURL. This gives you better control of the response and expectations.

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.

Conclusion

From the above, you can see how easy it is to send headers alongside your requests using cURL. It is important you know that cURL is more complex than it appears. It is a full-fledged tool and can send all kinds of requests even beyond the HTTP and HTTPS protocols. This command tool is what powers most of the critical hardware applications today.

FAQs

The cURL is a capable tool and does have the capability to read responses. You can read the content of all of the headers returned by your response. To print out the headers sent by your target site, just add the -I flag to your command. This is the format for doing that curl —I [URL]. You should replace the URL placeholder with an actual URL and you will see the response.

While the cURL tool is portable and versatile, it is not an easy-to-use tool. If all you need is a tool to test APIs, you are better off using some of the alternatives out there. Currently, some of the best alternatives to the cURL are Postman, HTTPie, JMeter, and SoapUI. They are an easy-to-use alternative and come with a user interface.

For some websites, if you send a web request without sending headers, your request will be blocked as it won’t appear to come from a legitimate source which are browser. Modification of headers is not illegal but allowed. However, the website does not support modification of some headers such as user agents. Be that as it may, there is nothing illegal about modifying headers including user agent headers.

Top

Top