How to Include Response Headers in Curl Output
When debugging web requests or testing APIs, you often need to view
the HTTP response headers alongside the actual response body. This
article explains how to instruct the curl command-line tool
to include response headers in its standard output using the
-i option, and covers alternative methods for viewing
header metadata depending on your specific troubleshooting needs.
Use the -i (or
--include) Option
To include the HTTP response headers in your normal output along with
the response body, use the -i or --include
option. This tells curl to print the protocol headers
before the actual document content.
Run the following command in your terminal:
curl -i https://api.github.comUnderstanding the Output
When you run this command, the output is split into two distinct parts:
- The Headers: At the very top, you will see the HTTP
status line (e.g.,
HTTP/2 200) followed by the response headers (such asContent-Type,Server,Date, etc.). - The Body: After the headers, there is a single blank line, followed immediately by the standard response payload (HTML, JSON, or plain text).
Alternative:
Show Headers Only with -I (or --head)
If you only want to inspect the response headers and do not want to
download or view the response body, use the uppercase -I or
--head option. This performs a HEAD request
instead of a GET request.
curl -I https://api.github.comNote: Some servers handle HEAD requests differently
than GET requests, but this is the fastest way to inspect
headers without downloading large files.
Alternative:
Verbose Mode with -v (or --verbose)
For deeper troubleshooting, the -v or
--verbose option prints the entire communication process.
This includes the SSL/TLS handshake, the request headers sent by your
machine (prefixed with >), and the response headers
received from the server (prefixed with <).
curl -v https://api.github.comUnlike -i, which prints the response headers directly to
standard output (stdout), verbose mode prints debugging information to
standard error (stderr), keeping your saved output file clean if you
redirect the response.