How to Send HTTP Headers With cURL

You can modify and send HTTP request headers using cURL. To find out how to do that, come in now and read the article below on how to send HTTP headers with cURL.

How to Send HTTP Headers With cURL

The cURL tool looks simple to many, but it is actually one of the most powerful tools created, serving as the Internet data transfer engine of over 20 million devices and software. If you make use of the Internet, then you are definitely making use of this technology. Interestingly, it is available as a command line tool. And this is why many underestimate its capabilities. As with other tools for sending web requests, cURL does send HTTP headers as your browser does anytime web requests are sent.

It sends HTTP headers with a user agent string to tell its target it's a cURL client, sends it with an Accept header to tell the target the type of content it accepts as a response, and sends the hostname of the target. Interestingly, these values it sends are not written on stones — they can be overridden and changed. You can also add new HTTP headers depending on the kind of requests you want to send. In this article, I will show you how to send HTTP headers and also modify them using cURL.


cURL Default HTTP Headers

YouTube video

The cURL tool, as stated in the introductory section, also sends headers alongside the requests. Without sending HTTP headers, most web servers will not respond with a 200 status code but instead return an error. By default, the HTTP headers are sent to use the real details of cURL. Currently, there are 3 major HTTP headers sent by cURL. This includes Host, User-Agent, and Accept. Below are the specific values of the HTTP headers.

> GET / HTTP/1.1

> Host: google.com

> User-Agent: curl/7.87.0

> Accept: */*

The above is the default HTTP  headers cURL will send if you send a request to Google. As you can see, the User-Agent string is ‘curl/7.87.0,” which tells Google or its target that the client is cURL and its specific version. The “*/*” for the Accept Header tells the target host that cURL accepts all response data types.

Now, the 3 above are the popular and default request HTTP headers sent for all types of requests. However, there are some other default headers sent depending on the request method. If you send a POST request with data, the “Content-Type” header with the type of content being sent is the value.

And remember, these are sent by default, you do not need to send them explicitly. But if you need to modify them, which you will have to do in most of your requests, then you need to send them explicitly. Below is how to send them.


How to Modify and Send HTTP Headers

Modify and Send HTTP Headers

If you don’t send HTTP headers explicitly, cURL will send them for you — and use the default values mentioned above. While this is not bad, it makes it difficult for you to test things out. And in some situations, your requests would even be denied. If you want to mimic certain user agents or send custom headers, then you need to state them explicitly. In this section, you will be shown how to send HTTP headers using cURL.

There are two ways you can send HTTP headers — you either send in inline or have it saved in a file and state the file. In either method, you need to use the — H tag, which is the header tag.


Send an HTTP Header Inline

Below is a sample command for sending headers.

curl -H "User-Agent: Mozilla/5.0" -H "Cookie: name=value" https://www.example.com

As you can see above, there are two headers defined — User-Agent and Cookie. Each has its own —H flag. After each of the flags, the header is defined using the format “Header-Name: Value.”

At first, this looks simple and good. But if you have to send multiple headers, it can be difficult to manage. This takes us to the next method of sending headers.


Sending HTTP Headers using a TXT file

The easier method of sending HTTP headers is by using a txt file. You should create a txt file named header.txt. each line should contain one header and its value. Example: User-Agent: Mozilla/5.0.

Once you are done, you can then use the following command to send the request with the headers.

curl -H "@headers.txt" https://www.example.com

FAQs

Q. Does cURL Support HTTP Headers?

Yes, the cURL command line tool or library comes with support for HTTP requests as they are an important part of a web request. As with other web clients, there are default headers and values depending on the request method being used. If you don’t send a request with headers explicitly stated, the tool will make use of the default headers and values for the specific request method. It is advisable to send HTTPs, especially if you want to mimic a different user client.

Q. Can the cURL User-Agent String Be Changed?

The user agent string of cURL is curl/7.87.0, with the version number changing depending on the version of the cURL you use. The user agent header is just one out of the numerous headers that can be set and modified. Using the —H tag and then customizing the User-Agent header, you are able to change the header. Interestingly, if changed, it won’t appear to your target as if you are using the cURL tool anymore.


Conclusion

From the above, you can see how easy it is to send HTTP requests and modify headers to suit the kind of requests you want to send. This is one of the features that make cURL a powerful tool — the ability to customize it the way you want. If you are not aware of the specific headers to send and their corresponding values for the request you need to send, you should use the network tab in your browser developer tool to find out this information.

Popular Proxy Resources