In this guide, we are going to discuss How to display request headers and response headers in cURL command. We will see several examples with different cURL command line attributes to display http headers.
1. Display Request and Response Headers
If you run cURL command in verbose mode with -v
or --verbose
, you gets lot more information in addition to request and response information. You can refer above image to understand the information curl command produce if you run in verbose mode.
1. Messages with <
are indicates Request Headers
2. Messages with >
indicates Response Headers.
Example:
curl -v https://jsonplaceholder.typicode.com/posts/2
2. Display only Request and Response Headers
To display on Request and Response Headers in cURL command, redirect stderr to stdout using 2>&1 and then us grep
command to filter the matched content.
Example:
curl -v https://dummyjson.com/products/1 2>&1 | grep '>\|<'
Results:
> GET /products/1 HTTP/1.1 > Host: dummyjson.com > User-Agent: curl/7.61.1 > Accept: */* > < HTTP/1.1 200 OK < Server: Cowboy < Connection: keep-alive < Access-Control-Allow-Origin: * < X-Dns-Prefetch-Control: off < X-Frame-Options: SAMEORIGIN < Strict-Transport-Security: max-age=15552000; includeSubDomains < X-Download-Options: noopen < X-Content-Type-Options: nosniff < X-Xss-Protection: 1; mode=block < X-Ratelimit-Limit: 120 < X-Ratelimit-Remaining: 119 < Date: Sun, 04 Sep 2022 17:03:06 GMT < X-Ratelimit-Reset: 1662311024 < Content-Type: application/json; charset=utf-8 < Content-Length: 525 < Etag: W/"20d-tt4NxYAcaT5EGoQEws9YK9ptHeU" < Vary: Accept-Encoding < Via: 1.1 vegur <
3. Display only Response Headers
To display only response header in cURL, either you can dump headers with -D
OR you can use --head
.
curl -s --head https://dummyjson.com/products/1
Results:
HTTP/1.1 200 OK Server: Cowboy Connection: keep-alive Access-Control-Allow-Origin: * X-Dns-Prefetch-Control: off X-Frame-Options: SAMEORIGIN Strict-Transport-Security: max-age=15552000; includeSubDomains X-Download-Options: noopen X-Content-Type-Options: nosniff X-Xss-Protection: 1; mode=block X-Ratelimit-Limit: 120 X-Ratelimit-Remaining: 119 Date: Sun, 04 Sep 2022 17:09:12 GMT X-Ratelimit-Reset: 1662311384 Content-Type: application/json; charset=utf-8 Content-Length: 525 Etag: W/"20d-tt4NxYAcaT5EGoQEws9YK9ptHeU" Vary: Accept-Encoding Via: 1.1 vegur
4. Display only Request Headers
To display only Request message headers, grep
command can be used to filter cURL command results.
Example:
curl -v https://dummyjson.com/products/1 2>&1 >/dev/null | grep '>'
Results:
> GET /products/1 HTTP/1.1 > Host: dummyjson.com > User-Agent: curl/7.61.1 > Accept: */* >
5. Conclusion
In this tutorial, we have seen several examples on How to display Request and Response headers in cURL.