How to display request and response headers with cURL

May 21, 2022 No comments curl headers verbose include

1. Introduction

The cURL is an open-source tool for transferring data using communication protocols such as FTP, HTTP, TELNET, and many more. In this article, we will describe how to display headers for requests and responses using cURL. HTTP headers allow passing additional information with the request or the response.

2. Display request/response headers using --verbose parameter

To present some extra information about HTTP operation, we can use the -v or --verbose parameter. The curl tool will display more data about requests and responses, and also will present HTTP headers used in communication.

This could be useful for debugging and checking what is going on "under the hood".

-v, --verbose

Let's check the -v parameter with an HTTP GET request to google.com website:

curl -v google.com

The output:

*   Trying 172.217.16.14:80...
* TCP_NODELAY set
* Connected to google.com (172.217.16.14) port 80 (#0)
> GET / HTTP/1.1
> Host: google.com
> User-Agent: curl/7.68.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 301 Moved Permanently
< Location: http://www.google.com/
< Content-Type: text/html; charset=UTF-8
< Date: Thu, 19 May 2022 21:22:37 GMT
< Expires: Sat, 18 Jun 2022 21:22:37 GMT
< Cache-Control: public, max-age=2592000
< Server: gws
< Content-Length: 219
< X-XSS-Protection: 0
< X-Frame-Options: SAMEORIGIN
< 
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>
* Connection #0 to host google.com left intact

Requests headers:

> Host: google.com
> User-Agent: curl/7.68.0
> Accept: */*

Response headers:

< Location: http://www.google.com/
< Content-Type: text/html; charset=UTF-8
< Date: Thu, 19 May 2022 21:22:37 GMT
< Expires: Sat, 18 Jun 2022 21:22:37 GMT
< Cache-Control: public, max-age=2592000
< Server: gws
< Content-Length: 219
< X-XSS-Protection: 0
< X-Frame-Options: SAMEORIGIN

2. Display response data with headers using cURL and -i parameter

With curl we also have an option to display only HTTP headers from the response.

To do so we need to use -i or --include parameter - cURL-i.

curl --include google.com

The output:

HTTP/1.1 301 Moved Permanently
Location: http://www.google.com/
Content-Type: text/html; charset=UTF-8
Date: Thu, 19 May 2022 21:29:12 GMT
Expires: Sat, 18 Jun 2022 21:29:12 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 219
X-XSS-Protection: 0
X-Frame-Options: SAMEORIGIN

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>

3. Display only headers using cURL with -I parameter

To fetch only the headers we could use -I or --head option - cURL-I.

curl -I google.com

The output:

HTTP/1.1 301 Moved Permanently
Location: http://www.google.com/
Content-Type: text/html; charset=UTF-8
Date: Thu, 19 May 2022 21:33:28 GMT
Expires: Sat, 18 Jun 2022 21:33:28 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 219
X-XSS-Protection: 0
X-Frame-Options: SAMEORIGIN

4. Conclusion

In this article, we present how to display response/request headers using cURL options.

{{ message }}

{{ 'Comments are closed.' | trans }}