How To Send/Set HTTP Header with curl Command?

HTTP or HTTPS is one of the most popular protocols used in IT and the web. HTTP/HTTPS is used to transmit data and communicate between client and server. Even HTTP and HTTPs are used via web browsers like Google Chrome, Mozilla Firefox, Windows Edge, Opera, etc. there are different command-line tools that can be used to run HTTP/HTTPS protocols.

curl Command Syntax

The curl command has very simple syntax where options are provided after the command and the last parameter is the URL we want to access.

curl OPTIONS URL
  • OPTIONS are used to provide extra information and data like HTTP header to the request.
  • URL is the web page or resource or service address we want to access.

HTTP Header

HTTP headers are used to provide information about the request like date, time, content type, language etc. While using a web browsers the HTTP headers are not shown to the user and only the HTML, CSS, JavaScript, images are shown. Most of the HTTP requests contains single or multiple headers like below.

POST /test/site/post.cgi HTTP/1.1
Host: example.com
User-Agent: moo
Shoesize: 12
Cookie: a=12; b=23
Content-Type: application/json
Content-Length: 57

{"I do not speak": "json"}
{"I do not write it": "either"}

Send HTML Header

curl command provides the -H option in order to provide HTTP headers. -H options can be used single or multiple times without problem. If same header is provided multiple times the last header value will be provided. In the following example we will provide the Cookie header.

$ curl -H "Cookie:a=12" https://www.linuxtect.com

Multiple headers can be provided with multiple -H options. Alternatively --header option can be also used instead of the -H option.

$ curl -H "Cookie: a=12" -H "Host: linuxtect.com" https://www.linuxtect.com

$ curl --header "Cookie: a=12" -H "Host: linuxtect.com" https://www.linuxtect.com

Send HTML Header For JSON

JSON is a popular data format mostly used to express data in a structured and human readable format for different applications and API. In order to work with JSON data servers the application/JSON mime type can be specified in HTTP header like below.

$ curl -H "Cookie: a=12" -H "Accept: application/json" https://www.linuxtect.com

Send HTML Header For XML

XML is another way to store structured and formatted data. XML is mainly used for web applications and APIs in order to put or get data from the application server. curl command can be used to query XML responses with the application/xml HTTP header like below.

$ curl -H "Cookie: a=12" -H "Accept: application/xml" https://www.linuxtect.com

Set User Agent

User agent information is important for the HTTP requests. The user agent name, family even version information is generally provided with the HTTP header. We can set the User Agent HTTP header like below.

$ curl -H "Agent: linuxtect" https://www.linuxtect.com

Post Data

Post data can be provided while using the HTTP headers. The --data option and data is provided as parameters to the curl command like below.

$ curl --data "test=1&deneme=2" https://www.linuxtect.com
READ  Linux rm Command Tutorial

Leave a Comment