Curl is a powerful command-line tool used for transferring data to and from a web server using various protocols, including HTTP, HTTPS, SCP, SFTP, TFTP, and more. When working with web servers, it’s often necessary to inspect the request and response headers to diagnose issues or understand how the server is responding to your requests. In this tutorial, we’ll explore how to use curl to display both request and response headers.
Displaying Request Headers
To display the request headers sent by curl, you can use the -v
or --verbose
option. This option tells curl to be more verbose and show more detailed information about the transfer, including the request headers.
Here’s an example of how to use the -v
option:
curl -v http://example.com
This will display the request headers sent by curl, along with the response from the server. The output will look something like this:
> GET / HTTP/1.1
> Host: example.com
> User-Agent: curl/7.64.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: text/html; charset=UTF-8
< Date: Wed, 21 Oct 2020 14:30:00 GMT
< Server: Apache
As you can see, the request headers are displayed above the response headers.
Displaying Response Headers
To display only the response headers, you can use the -D
option followed by a hyphen (-
) to send the output to stdout. Here’s an example:
curl -s -D - -o /dev/null http://example.com
This will display only the response headers from the server.
Following Redirects
When working with redirects, you may want to follow the redirect chain and display the headers for each request. You can use the -L
option to tell curl to follow redirects. Here’s an example:
curl -Lvso /dev/null http://example.com
This will display the request and response headers for each request in the redirect chain.
Using the --trace-ascii
Option
For even more detailed output, you can use the --trace-ascii
option to display the entire conversation between curl and the server, including the request and response headers. Here’s an example:
curl --trace-ascii - http://example.com
This will display a detailed log of the transfer, including the request and response headers.
Conclusion
In this tutorial, we’ve explored how to use curl to display both request and response headers. By using the -v
, -D
, -L
, and --trace-ascii
options, you can gain valuable insights into the communication between your client and the server. Whether you’re debugging issues or simply curious about how the web works, curl is an essential tool for any web developer or administrator.