How to Send a Header Using a HTTP Request Through a cURL call?

HTTP and cURL are the two basic components of web-development and networking. Especially in web scraping, to communicate with the different websites, HTTP requests are sent to them using the cURL. These HTTP requests contain the HTTP headers that transfer the information between the web servers and clients.

This article will explain the HTTP headers and cURL calls. Also, it will explore the methods of sending a header using an HTTP request through a cURL call.

What is the HTTP Header?

Hypertext Transfer Protocol headers are essential components of HTTP communication. They transfer information between the client such as browsers and web servers, also there are two types of HTTP headers:

  • Request Headers: These provide information about the client including its identity
  • Response Headers: These provide information about the server’s response including its location

The basic components of the HTTP headers are:

  • It contains Name-Value pairs
  • They can contain customized information or may contain the keyword “Authorization” if some authentication is required
  • To store the activity history and information, cookies are stored in the HTTP headers
  • Moreover, headers may contain the keywords such as “Accept”, “Key-Type”, and “Location” for different purposes

What is the cURL?

Client URL (cURL) is the command line utility that sends HTTP requests to different web-related servers. It includes several protocols such as HTTP, HTTPS, and FTPS that help in different web development-related tasks. To install the cURL on Linux distributions, the following reading is recommended. 

After ensuring the installation of the curl command utility, it can be used to send HTTP requests to different websites with the following mentioned general syntax:

$ curl [options] [website]

According to the above general syntax, the curl command will extract the information about the specified website. 

What is the Method to Send the Headers Using an HTTP Request Through a cURL Call?

The headers are sent using the HTTP request through a cURL call for sending the additional information to the web servers. Mostly the headers are used to secure the cURL requests by using the “Authorization” keyword. 

The general syntax of sending the HTTP headers using the cURL is:

$ curl -H "HeaderName: HeaderValue" [URL Address]

In the above general syntax, “–header” can also be used instead of the “-H” to specify the header. Then define the header name and its value that is supposed to be sent through the HTTP header using the cURL call. And finally, type the URL address where the HTTP request is supposed to be sent. 

What are the Examples of Sending a Header Using an HTTP Request Through a cURL Call?

The examples explained in this section will explain some common uses to send a header using HTTP requests through a cURL call. 

Example 1: Headers for Authentication

Some of the websites restrict access without providing their specific credentials. This specific information can be sent using the HTTP headers using the cURL call. For this purpose, use the below-mentioned syntax:

$ curl -H "Authorization: "Credentials" " [URL Address]

Example 2: Headers for Custom User-Agent 

To set the headers for the custom user-agent, execute the command following the syntax explained:

$ curl -H "User-Agent: "Enter your User Agent" " [URL Address]

Example 3: Headers to Send Cookies

The cookies are sent through the HTTP requests to continue the session with the current website with the following general syntax:

$ curl -H "Cookie: Cookie_ID=00000" [URL Address]

Example 4: Headers for Referrer

These referrers are used to specify the website address from which users are directed to the current website:

$ curl -H [Referrer Website URL] [Current Website URL]

Example 5: Send Headers to Multiple Resources

The last example is to send the single HTTP header request to multiple resources:

$ curl -H "Range: bytes=0-1023" [URL Address]

These examples will help in understanding the methods of sending a header using HTTP requests through a cURL call. 

Conclusion

To send a header using an HTTP request through a cURL call, install the curl command and use its “H” or “Headers” option. This blog explains the HTTP, cURL command, and examples to understand the usage of the HTTP header requests through a cURL call.