When making HTTP requests using cURL, it is often useful to view the sent request headers to debug or better understand the exchange of data between the client and server. HTTP headers contain metadata about the request, like the client's user agent, accepted content types, and cookies being sent.

Understanding request headers is essential when diagnosing potential issues, simulating specific client behaviors, or verifying that the right parameters are being passed to the server. By default, cURL only displays the response body in the terminal. However, cURL provides mechanisms to view both the request headers sent to the server and the response headers received from the server.

Displaying the request headers while making HTTP requests with cURL offers deeper insight into how servers might interpret and handle your requests. It's also a handy tool for web development, system administration, and security assessments.

Steps to view HTTP request headers with cURL:

  1. Open the terminal.
  2. Make a basic cURL request to a target URL.

    $ curl http://onlineweb.tools
    
    <!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://onlineweb.tools
    *   Trying 127.0.0.1:80...
    * Connected to onlineweb.tools (127.0.0.1) port 80 (#0)
    > GET / HTTP/1.1
    > Host: onlineweb.tools
    > 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://onlineweb.tools > /dev/null 2>&1 | grep '^>'
    > GET / HTTP/1.1
    > Host: onlineweb.tools
    > 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://onlineweb.tools > /dev/null 2>&1 | grep '^>'
    > POST / HTTP/1.1
    > Host: onlineweb.tools
    > 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://onlineweb.tools > /dev/null 2>&1 | grep '^>' > headers-output.txt
  7. View saved request headers.

    cat headers-output.txt
    > POST / HTTP/1.1
    > Host: onlineweb.tools
    > User-Agent: curl/8.1.2
    > Accept: */*
    > X-Custom-Header: TestValue
    >