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.comBreaking Down the Command Options
Understanding what each flag does helps you customize the command for your specific needs:
--spider: Tellswgetto act as a web spider. It checks the page’s existence and fetches the headers but does not download the actual web page content (the HTML body).--save-headers: Instructswgetto include the HTTP headers in the output stream.-q(or--quiet): Turns offwget’s default progress bar and logging output, ensuring your final file contains only clean header information.-O headers.txt: Redirects the output to a specific file name (in this case,headers.txt). Using an uppercase-Ooverwrites the file if it already exists.
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.comIn 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.