Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Get HTTP Header Info from Websites Using curl

· 5 min read · Home Tech

In this article
  1. What Are HTTP Headers and Why Check Them?
  2. The Basic curl Command: -I and --head
  3. Using -D to Save Headers to a File
  4. Getting Headers for a GET Request (Not Just HEAD)
  5. Following Redirects and Viewing All Headers
  6. Common Problems and How to Solve Them
  7. Comparing curl with Other Tools
  8. Frequently Asked Questions

To get HTTP header information from a website using curl, run curl -I https://example.com (or curl --head). This sends a HEAD request and prints the response headers, showing details like the server type, content type, caching rules, and status code.

What Are HTTP Headers and Why Check Them?

HTTP headers are metadata sent by a server alongside a web page or resource. They tell the client (your browser, curl, etc.) how to interpret the response: the status code (e.g., 200 OK, 404 Not Found), the content type (e.g., text/html), the content length, caching directives, and security policies like Strict-Transport-Security or Content-Security-Policy.

Checking headers is useful for debugging website issues (like redirects or missing cache headers), verifying that a server is responding correctly, or inspecting whether security headers are present. Curl is a command-line tool available on Linux, macOS, and Windows (Windows 10 and later includes it by default), making it a quick way to inspect headers without a browser.

The Basic curl Command: -I and --head

The simplest command is curl -I https://example.com. The -I flag (or the long form --head) tells curl to make a HEAD request, which fetches only the headers, not the body. This is fast and uses less bandwidth.

Example output for curl -I https://example.com might look like:

HTTP/2 200
content-type: text/html; charset=UTF-8
content-length: 1256
date: Wed, 04 Oct 2023 12:00:00 GMT
server: Apache
last-modified: Tue, 03 Oct 2023 10:00:00 GMT
etag: "abc123"
cache-control: max-age=604800

Note that the output format can vary depending on the server and the HTTP version (HTTP/1.1 vs HTTP/2).

Using -D to Save Headers to a File

If you need to keep a record of the headers, use -D (or --dump-header) to write them to a file. For example:

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

This downloads the page body to /dev/null (discarding it) and saves the headers to headers.txt. If you omit -o /dev/null, curl will print the body to the terminal, which can be messy for large pages.

Getting Headers for a GET Request (Not Just HEAD)

Sometimes a server responds differently to HEAD requests than to GET requests. Some sites might block HEAD or return different headers. To see the headers of an actual GET request, use -D with -o /dev/null as above. The -D - option prints headers to stdout:

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

This shows the headers for a GET request without printing the body. This is especially useful when you need to see headers like Content-Length or Content-Encoding that are accurate for the full response.

Following Redirects and Viewing All Headers

Websites often redirect (e.g., from http:// to https://, or from example.com to www.example.com). By default, curl does not follow redirects; it shows the headers of the initial response, which will include a Location header but not the headers of the final page.

To follow redirects and see the headers of each step, use -L (follow redirects) combined with -D -:

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

This prints all headers for each request in the redirect chain. If you want to see only the final headers, you can use -w to write the final URL and status code, but that is more advanced.

Common Problems and How to Solve Them

  • curl: (60) SSL certificate problem – This happens when the server's certificate is invalid or self-signed. To bypass verification (only for testing), use -k (or --insecure). Do not use this in production scripts.
  • curl: (6) Could not resolve host – The domain name does not exist or DNS is failing. Check the URL for typos.
  • curl: (7) Failed to connect – The server is unreachable, possibly due to network issues or the server being down.
  • No headers shown – If you use -I and get nothing, the server might not support HEAD requests. Try -D - with -o /dev/null instead.
  • HTTP/2 200 – The output format for HTTP/2 is different; headers are shown in lowercase. This is normal.

Comparing curl with Other Tools

While curl is the most common command-line tool, there are alternatives. wget --server-response shows headers but is less flexible. Browser developer tools (F12) show headers in the Network tab, which is useful for checking requests made by a page. Online tools like httpbin.org/headers can echo headers back to you, but they do not show the response headers of the target site.

For scripting and automation, curl is the best choice because it is widely available, has a simple syntax, and can be easily integrated into shell scripts.

Frequently Asked Questions

What is the difference between -I and -X HEAD?

-I (or --head) sends a HEAD request, which is the correct method to retrieve headers. -X HEAD forces curl to use the HEAD method, but it does not automatically suppress the body; you still need -o /dev/null to avoid printing the body. In practice, -I is simpler and recommended.

Can I see the request headers that curl sends?

Yes, use -v (verbose) or --trace-ascii - to see the request headers that curl sends to the server. This is useful for debugging when a server behaves unexpectedly.

How do I get only the status code?

Use -o /dev/null -w "%{http_code}". For example: curl -o /dev/null -w "%{http_code}" https://example.com. This prints just the numeric status code (e.g., 200).

Does curl work on Windows?

Yes, Windows 10 and 11 include curl.exe in the command prompt. If you have an older version, you can download it from the official curl website or use the Windows Subsystem for Linux (WSL).

Why do I see different headers in a browser?

Browsers send additional headers like User-Agent, Accept, and Cookie. Servers may respond differently based on these. To mimic a browser, set a User-Agent with -A (e.g., curl -A "Mozilla/5.0" -I https://example.com).