Response headers in HTTP communications contain key metadata about the server’s response, including status codes, content types, caching policies, and cookies. They help clarify how data is delivered and can indicate where to troubleshoot if issues arise.

By default, cURL displays only the response body and omits headers. Using options like --include or --head reveals these headers, allowing a more thorough examination of server output and behavior.

Carefully inspecting headers provides insights into content negotiation, performance optimizations, and correct server configuration. Observing these details enables informed adjustments to server settings or client requests.

Steps to show HTTP response headers in cURL:

  1. Open a terminal.
  2. Use --include to show response headers along with the body.
    $ curl --include "http://example.com"
    HTTP/1.1 200 OK
    Date: Tue, 26 Sep 2023 08:36:01 GMT
    Server: Apache
    X-Powered-By: PHP/8.2.10
    Content-Type: text/html; charset=UTF-8
    Transfer-Encoding: chunked
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Example Domain</title>
    </head>

    --include prints response headers followed by the body.

  3. Show only headers with --head.
    $ curl --head "http://example.com"
    HTTP/1.1 200 OK
    Date: Tue, 26 Sep 2023 08:36:46 GMT
    Server: Apache
    X-Powered-By: PHP/8.2.10
    Content-Type: text/html; charset=UTF-8

    --head sends a HEAD request, returning headers without the body.

  4. Use --verbose to view both request and response headers.
    $ curl --verbose "http://example.com"
    *   Trying 93.184.216.34:80...
    * Connected to example.com (93.184.216.34) port 80 (#0)
    > GET / HTTP/1.1
    > Host: example.com
    > User-Agent: curl/7.68.0
    < HTTP/1.1 200 OK
    < Date: Tue, 26 Sep 2023 08:37:09 GMT
    < Server: Apache
    < X-Powered-By: PHP/8.2.10
    < Content-Type: text/html; charset=utf-8

    Lines starting with < represent response headers.

  5. Save response headers to a file for later review.
    $ curl --head "http://example.com" --output response-headers.txt

    Review the saved file to confirm server behavior or diagnose issues.

  6. Inspect the stored headers to confirm expected fields are present.
Discuss the article:

Comment anonymously. Login not required.