HTTP headers are parts of HTTP requests that carry information about the request, such as the type of browser being used, what kinds of data the client can receive, and any cookies sent.

When using cURL to make HTTP requests, it's useful to see the request headers that are sent. This can help troubleshoot, understand how data is exchanged between the client and server, and ensure the correct information is sent.

Typically, cURL shows only the server's response. However, there is an option in cURL also to display the request headers sent to the server along with the server's responses.

Steps to view HTTP request headers with cURL:

  1. Open the terminal.
  2. Make a basic cURL request to a target URL.
    $ curl http://www.simplified.guide
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
    
    
        <meta charset="utf-8">
        <title> Simplified Guide </title>
    ##### snipped
  3. Add the -v or --verbose flag to the cURL command to display the request and response headers.
    $ curl -v http://www.simplified.guide
    *   Trying 127.0.0.1:80...
    * Connected to www.simplified.guide (127.0.0.1) port 80 (#0)
    > GET / HTTP/1.1
    > Host: www.simplified.guide
    > User-Agent: curl/8.1.2
    > Accept: */*
    >
    < HTTP/1.1 200 OK
    < Date: Tue, 26 Sep 2023 09:00:46 GMT
    < Server: Apache
    < X-Powered-By: PHP/8.2.10
    < Vary: Cookie
    < Set-Cookie: DokuWiki=ib5a5d9nf6i398lc0t71f0cjv3; path=/; HttpOnly
    < Expires: Thu, 19 Nov 1981 08:52:00 GMT
    < Cache-Control: no-store, no-cache, must-revalidate
    < Pragma: no-cache
    < Set-Cookie: DW68700bfd16c2027de7de74a5a8202a6f=deleted; expires=Thu, 01 Jan 1970 00:00:01 GMT; Max-Age=0; path=/; HttpOnly
    < Transfer-Encoding: chunked
    < Content-Type: text/html; charset=utf-8
    <
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
    
    
        <meta charset="utf-8">
        <title> Simplified Guide </title>

    Lines starting with > are the request headers.

  4. Display only the request headers.
    $ curl -v http://www.simplified.guide > /dev/null 2>&1 | grep '^>'
    > GET / HTTP/1.1
    > Host: www.simplified.guide
    > User-Agent: curl/8.1.2
    > Accept: */*
    >

    This command discards the actual response and filters only the request headers using the grep command.

  5. Use a custom request method or add additional headers to view them.
    $ curl -v -H "X-Custom-Header: TestValue" -X POST http://www.simplified.guide > /dev/null 2>&1 | grep '^>'
    > POST / HTTP/1.1
    > Host: www.simplified.guide
    > User-Agent: curl/8.1.2
    > Accept: */*
    > X-Custom-Header: TestValue

    The -H option allows adding custom headers, while -X specifies the HTTP method to use.

  6. Redirect the output to a file to avoid missing headers for lengthy outputs.
    $ curl -v -H "X-Custom-Header: TestValue" -X POST http://www.simplified.guide > /dev/null 2>&1 | grep '^>' > headers-output.txt
  7. View saved request headers.
    cat headers-output.txt
    > POST / HTTP/1.1
    > Host: www.simplified.guide
    > User-Agent: curl/8.1.2
    > Accept: */*
    > X-Custom-Header: TestValue
    >
Discuss the article:

Comment anonymously. Login not required.