How to Save HTTP Response Headers to a File Using Wget?

This article provides a quick overview and step-by-step guide on how to capture and save the HTTP response headers sent by a server into a local file using the wget command-line utility. You will learn the specific command-line flags required to isolate header data, look at practical examples, and understand how to append or overwrite the output files during your web scraping or troubleshooting workflows.

The Standard Command to Save Headers

To save the HTTP headers directly to a file without downloading the actual webpage content, combine the --save-headers option with the output redirection flag. However, because wget normally downloads the body of the file, you should pair it with the spider option if you only want the headers.

Here is the standard syntax:

wget --spider --save-headers -q -O headers.txt http://example.com

Breaking Down the Command Options

Understanding what each flag does helps you customize the command for your specific needs:

Alternative Method: Using Server Response Logging

If you want to view the entire transaction log—including the request sent by your machine and the exact response headers from the server—you can redirect wget’s internal log output instead.

wget -o log.txt --spider http://example.com

In this variation, the lowercase -o captures the entire standard error stream (where wget prints its connection data) and saves it to log.txt. This file will contain the HTTP status codes, server type, content length, and timestamps.