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:

  1. Open a terminal.
  2. 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.

  3. Filter request headers with grep.
    $ curl --verbose http://example.com | grep '^>'

    Extract only lines starting with > to isolate request headers.

  4. 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.

  5. 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.

  6. 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.

Discuss the article:

Comment anonymously. Login not required.