HomecurlcURL - How to send header in HTTP Request?

cURL – How to send header in HTTP Request?

I this guide we discuss how to send single and multiple headers in http request with cURL command.

1. cURL default Headers

By default cURL includes some headers internally for each http request. Refer to see only headers display request and response headers in cURL.

$ curl -v https://jsonplaceholder.typicode.com/posts/1 2>&1 >/dev/null | grep '>'

> GET /posts/1 HTTP/2
> Host: jsonplaceholder.typicode.com
> user-agent: curl/7.81.0
> accept: */*
>

2. Sending Custom Header

cURL provides -H or –header options to send custom headers in http request. You can send headers in cURL command like below example.

curl -H "Content-Type: application/json" https://jsonplaceholder.typicode.com/posts/1 Let’s see only headers in the results with example.

$ curl -H "Content-Type: application/json" -v https://jsonplaceholder.typicode.com/posts/1 2>&1 >/dev/null | grep '>'

> GET /posts/1 HTTP/2
> Host: jsonplaceholder.typicode.com
> user-agent: curl/7.81.0
> accept: */*
> content-type: application/json
>

3. Sending Multiple headers

You need to include multiple -H or –header options to send Multiple headers in the request.

curl --header "Content-Type: application/json" \
--header "Message-Secret: The world of Dev" \
--header "User-Agent: Online-cURL" \
https://jsonplaceholder.typicode.com/posts/1

Example:

$ curl --header "Content-Type: application/json" \
     --header "Message-Secret: The world of Dev" \
     --header "User-Agent: Online-cURL" \
     -v https://jsonplaceholder.typicode.com/posts/1 2>&1 >/dev/null | grep '>'

> GET /posts/1 HTTP/2
> Host: jsonplaceholder.typicode.com
> accept: */*
> content-type: application/json
> message-secret: The world of Dev
> user-agent: Online-cURL
>

4. Removing / Overriding Header value

4.1. To remove http header value that send by cURL by default, you need to provide no value.

$ curl -H "Host:" -v https://jsonplaceholder.typicode.com/posts/1 2>&1 >/dev/null | grep '>'

> GET /posts/1 HTTP/2
> user-agent: curl/7.81.0
> accept: */*
>

4.2. To override the header value, simply need to provide a header value in the command.

$ curl -H "Host: javabydeveloper.com" -v https://jsonplaceholder.typicode.com/posts/1 2>&1 >/dev/null | grep '>'

> GET /posts/1 HTTP/2
> Host: javabydeveloper.com
> user-agent: curl/7.81.0
> accept: */*
>

4.3. To provide custom header with no value, use ; instead : in the header.

$ curl -H "Message-Secret;" -v https://jsonplaceholder.typicode.com/posts/1 2>&1 >/dev/null | grep '>'

> GET /posts/1 HTTP/2
> Host: jsonplaceholder.typicode.com
> user-agent: curl/7.81.0
> accept: */*
> message-secret:
>

5. Read Headers from the file

Using syntax @{filename}, you can provide headers from the file. cURL reads file and takes each line as header name and value.

$ curl -v -H @/home/my-headers.txt https://jsonplaceholder.typicode.com/posts/1 2>&1 >/dev/null | grep '>'

> GET /posts/1 HTTP/2
> Host: jsonplaceholder.typicode.com
> accept: */*
> appname: javabydeveloper
> type: Programming Tecnologies
> header-source: file
> user-agent: Online-cURL
> author: "Java Developer"
> message-secret: "World of Dev"
>

Reading headers from file in Windows example:

curl -v -H @C:\\Documents\\my-headers.txt -v https://jsonplaceholder.typicode.com/posts/1 2>&1 >/dev/null | grep '>'

6. Conclusion

In this tutorial we have seen, how to send headers in cURL http request with various examples.

7. References

  1. How to display request headers and response headers in cURL
  2. man curl
  3. cURL GET Request
  4. pretty print JSON in cURL

LEAVE A REPLY

Please enter your comment!
Please enter your name here