How to Show Only HTTP Headers with cURL

When debugging web applications or analyzing server behavior, you often need to inspect the HTTP response headers without downloading the entire response body. This article demonstrates how to use the curl command-line tool to retrieve and display only the HTTP response headers quickly and efficiently using different command-line flags.

Method 1: Using the HEAD Request (-I or --head)

The easiest and most common way to view only the HTTP headers is by using the -I (or --head) option. This instructs curl to send a HEAD request to the server instead of a GET request. The server will respond with only the headers, and no response body will be transmitted.

Run the following command in your terminal:

curl -I https://example.com

Method 2: Fetching Headers with a GET Request

Some web servers or API endpoints do not support HEAD requests, or they may return a different status code (like 405 Method Not Allowed) when a HEAD request is made. If you need to send a standard GET request but still only want to output the headers, you can combine a few flags to dump the headers and discard the response body.

Run this command:

curl -s -D - -o /dev/null https://example.com

Here is how this command works: * -s (Silent): Hides the progress meter and error messages. * -D - (Dump Header): Dumps the response headers to a specified file. Using the hyphen - directs the output to standard output (your terminal screen). * -o /dev/null (Output): Redirects the actual response body to /dev/null, effectively discarding it.