How to Use the curl Command to Check the HTTP Response Headers of a Website

When you type a URL into a web browser, the server sends back much more than just the HTML code that renders the webpage. Before the visual content is transmitted, the server sends a block of metadata known as HTTP Response Headers.

These headers contain critical diagnostic information, including the server type (e.g., Nginx or Apache), caching policies, security protocols, redirection rules (like 301 redirects), and the exact HTTP status code (like 200 OK or 404 Not Found).

If you are a web developer or system administrator troubleshooting a website issue, relying on a browser’s Developer Tools can be slow and caching can skew your results. The fastest, most precise way to view raw HTTP headers is directly from the Linux terminal using the curl command.

The Standard Command: Fetching Only Headers

By default, if you run curl https://example.com, the terminal will dump the entire raw HTML source code of the webpage onto your screen, which is chaotic and unhelpful if you only care about the server response.

To tell curl to fetch only the HTTP headers and completely ignore the HTML body, you use the -I (capital ‘i’) flag.

curl -I https://digitash.com

When you run this, you will get a clean, readable output that looks similar to this:

HTTP/2 200 
server: nginx
date: Wed, 15 Aug 2024 10:30:00 GMT
content-type: text/html; charset=UTF-8
cache-control: max-age=3600
strict-transport-security: max-age=31536000
x-frame-options: SAMEORIGIN

The very first line (HTTP/2 200) immediately confirms that the server is using the modern HTTP/2 protocol and the page loaded successfully (Status 200).

Following Redirects (The -L Flag)

One of the most common reasons to check headers is to troubleshoot redirection loops. For example, if you query http://example.com (without the ‘s’), you want to verify that the server is issuing a 301 Permanent Redirect to the secure HTTPS version.

If you run curl -I on a redirected URL, it will simply show you the 301 status and stop. It will not show you the headers of the final destination page. To force curl to follow the redirect chain to the very end, add the -L flag.

curl -I -L http://digitash.com

This command will output two blocks of headers: first, the 301 Redirect response showing the location: it is pushing you towards, and second, the 200 OK response from the final, secure destination.

Seeing Both Headers and Content (The -i Flag)

If you are debugging a REST API and need to see both the headers and the JSON output body in a single request, the capital -I flag won’t work because it explicitly drops the body.

Instead, use the lowercase -i flag.

curl -i https://api.example.com/data

This will print the full HTTP response headers at the top of your terminal, followed immediately by a blank line, and then the full HTML or JSON payload of the requested resource.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.