How to get only response header from HTTP GET using cURL

September 11, 2022 No comments curl GET header response

1. Introduction

In this short article, we are going to show how to print HTTP headers from GET requests using curl.

2. The -I, --head parameter

If we want to retrieve only the headers using curl, an additional parameter -I or --head is required. This method uses a special HEAD command, which is (should) be available on HTTP servers and is used to get only a document's header.

curl -s -I GET

In the following example, we also used the silent mode to hide any additional information, and progress bars used by curl.

The output:

HTTP/1.1 200 OK
Date: Sun, 11 Sep 2022 12:08:18 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
Server: gws
X-XSS-Protection: 0
X-Frame-Options: SAMEORIGIN
Accept-Ranges: none
Vary: Accept-Encoding
Transfer-Encoding: chunked

3. Get response headers using -D, --dump-header <filename> parameter

We could also write the headers that were received to the designated file. The usage of this option will result in the creation of an empty file if no headers are received.

curl --dump-header headers.txt

The headers.txt file contains the following content:

HTTP/2 200 
date: Sun, 11 Sep 2022 15:04:15 GMT
expires: -1
cache-control: private, max-age=0
content-type: text/html; charset=ISO-8859-1
p3p: CP="This is not a P3P policy! See for more info."
server: gws
x-xss-protection: 0
x-frame-options: SAMEORIGIN
alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"
accept-ranges: none
vary: Accept-Encoding

And if we don't want to print output HTML in the console we could use the following command:

curl -sS --dump-header headers.txt -o /dev/null

This example will call the endpoint in silent mode (-sS param) and redirects the output to /dev/null.

4. Conclusion

In this short article, we presented a method to show only headers from HTTP GET requests using the cURL tool. Note that the server may occasionally deliver POST and HEAD headers with different information. But in most cases, these headers should be always the same.

{{ message }}

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