How to view HTTP response headers when using cURL
cURL is widely used by developers, system administrators, and website owners to interact with web servers, make API requests, download files, and much more. When making requests, web servers don't just send back the data or content requested – they also send a set of HTTP headers. These headers offer valuable server response information, including meta-information, status, caching rules, and more.
Understanding HTTP headers is essential when diagnosing issues, optimizing performance, or inspecting security parameters. For instance, headers can give insights into server types, cache policies, security configurations, and redirection logic.
With cURL, you can easily fetch and examine these headers to better understand the behavior of web servers or applications. By default, cURL only displays the body of the response. However, you can instruct it to display headers for a more in-depth look into the server's response.
Steps to view HTTP response headers using cURL:
-
Open the terminal or command prompt on your computer.
-
Make a standard cURL request to a desired URL.
$ curl onlineweb.tools <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title> Simplified Guide </title> ##### snipped
Note that only the response body or content is displayed.
-
View the headers and the content using the -i or –include option.
$ curl -i onlineweb.tools HTTP/1.1 200 OK Date: Tue, 26 Sep 2023 08:36:01 GMT Server: Apache X-Powered-By: PHP/8.2.10 Vary: Cookie Set-Cookie: DokuWiki=ka1l9l3j8d7i6rpj3ia3ppb13d; 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> ##### snipped
-
Only see headers and exclude the body using the -I or –head option.
$ curl -I onlineweb.tools HTTP/1.1 200 OK Date: Tue, 26 Sep 2023 08:36:46 GMT Server: Apache X-Powered-By: PHP/8.2.10 Vary: Cookie Set-Cookie: DokuWiki=bin6f85ecq6bhlltr3vss3hm66; 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 Content-Type: text/html; charset=utf-8
-
View verbose output, including request and response headers, using the -v or –verbose option.
$ curl -v 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 08:37:09 GMT < Server: Apache < X-Powered-By: PHP/8.2.10 < Vary: Cookie < Set-Cookie: DokuWiki=3uod8b4v0n3fmvi1aj6r3c0vp0; 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> ##### snippped
Verbose mode is particularly useful for debugging connections, certificate issues, or understanding the full transaction process.
-
Save the headers to a file using the -o or –output option followed by the filename.
$ curl -I onlineweb.tools -o headers.txt % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
This method is helpful when dealing with extensive headers or when you want to document server responses.
-
Examine the saved request header file.
$ cat 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 Vary: Cookie Set-Cookie: DokuWiki=1t1e1l3gn9qnm37c0j7dj01hki; 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 Content-Type: text/html; charset=utf-8