Examining request headers reveals methods, hostnames, user agents, and custom fields sent to the server. This is crucial for debugging, verifying authentication headers, or ensuring correct content types.
By default, cURL shows only response bodies, but using --verbose or additional tools exposes the sent headers. Filtering request headers can be done with external commands like grep.
Viewing request headers validates request formatting and aids in diagnosing issues. It ensures that all required headers are present and correct before troubleshooting server responses.
Steps to view HTTP request headers in cURL:
- Open a terminal.
- Make a request with --verbose to see request headers.
$ curl "http://example.com" --verbose > GET / HTTP/1.1 > Host: example.com > User-Agent: curl/... > Accept: */*
--verbose shows lines beginning with > as request headers.
- Filter request headers with grep.
$ curl --verbose http://example.com | grep '^>'
Extract only lines starting with > to isolate request headers.
- Add custom headers and verify them in the output.
$ curl --header "X-Test: Value" "http://example.com" --verbose > X-Test: Value
Check that custom headers appear as intended.
- Change the request method and observe headers again.
$ curl --request POST "http://example.com" --verbose > POST / HTTP/1.1
--request shows changes in the HTTP method header.
- Save the verbose output to a file for later review.
$ curl --verbose "http://example.com" --output request-headers.txt
Offline analysis of request headers is possible by reviewing saved output.
Mohd Shakir Zakaria is an experienced cloud architect with a strong development and open-source advocacy background. He boasts multiple certifications in AWS, Red Hat, VMware, ITIL, and Linux, underscoring his expertise in cloud architecture and system administration.
Comment anonymously. Login not required.