HTTP response headers provide essential details about a server's response, such as status codes, caching, and content type. Understanding these headers can help troubleshoot issues, optimize performance, and analyze server responses.
By default, cURL does not show response headers, but it can be configured to display them alongside the response body or even show headers only. This is useful for debugging and verifying server information.
With cURL, you can display, filter, and save response headers as needed. This allows you to view details such as caching rules, cookies, and authentication responses in HTTP communication.
Steps to show HTTP response headers in cURL:
- Make a basic cURL request to any URL.
$ curl http://example.com <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Example Domain</title> </head>
The default cURL output displays only the response body.
- Show response headers and content using the -i option.
$ curl -i 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>
The -i option shows both the response headers and the response body.
- Show only the response headers with the -I option.
$ curl -I 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
The -I option shows only the headers, excluding the body.
- Show response headers using verbose mode.
$ curl -v 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
Use the -v option to display both request and response headers, useful for debugging.
- Show response headers without caching using the --no-cache option.
$ curl --no-cache -I 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 Cache-Control: no-store, no-cache, must-revalidate
The --no-cache option disables caching and ensures fresh data from the server.
- Save response headers to a file using the -o option.
$ curl -I http://example.com -o response-headers.txt
Use -o to save response headers to a file for later review.
- View the saved response headers.
$ cat response-headers.txt HTTP/1.1 200 OK Date: Tue, 26 Sep 2023 08:40:00 GMT Server: Apache X-Powered-By: PHP/8.2.10 Content-Type: text/html; charset=UTF-8
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.