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.
In the following example, we also used the silent mode to hide any additional information, and progress bars used by curl.
The output:
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.
The headers.txt
file contains the following content:
And if we don't want to print output HTML in the console we could use the following command:
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.
{{ 'Comments (%count%)' | trans {count:count} }}
{{ 'Comments are closed.' | trans }}