View Only Headers with Curl
When working with curl, it can give lots of excellent and detailed information, but sometimes it is too much! Today I’m fiddling with the caching headers on a client’s application, so I’m only interested in seeing the headers and not the body of the response. Making a
HEAD
request changes the output I get, so I really do want to GET
and then only see the headers.
Handily, when you use the -v
verbose flag with curl, it sends the output to stdout as usual, but the extra information including the headers goes to stderr. This means that I can therefore view the headers only throwing away stdout completely:
curl -v -s http://awesome-site.com 1> /dev/null
(you need the -s
to stop curl from “helpfully” printing progress bars as well)
Nice hack! :) But I think you can simplify it a lot.
$ curl -I -X GET http://awesome-site.com/
Or just use curl –head (curl -I is its shorter brother).
curl – I http://example.com
isn’t that enough?
Actually the HEAD request can sometimes be different to a GET – and my trick above works for POST and other verbs as well. So yes, curl -I will take you some distance, but sometimes you need to see the headers of a request you’re actually making, rather than crafting a new one.
Which is why you add the -X GET, -X POST, etc, parameter, to switch cURL from defaulting to HEAD.
This seems to be the best option. Summarized:
[code]curl -IXGET http://example.org/%5B/code%5D
Source: https://stackoverflow.com/a/49669324/429091
Use “curl -IL [URL]” for redirection following….
Just what I was looking for – worked as needed. Those other comments about using “-l” in different ways didn’t get the response headers at all.
This is great! The other “simplified” versions in the comments do not actually work in the same way, as they only print response headers, not require+response headers