Curl "write out" value of specific header
You can print a specific header with a single sed or awk command, but HTTP headers use CRLF line endings.
curl -sI stackoverflow.com | tr -d '\r' | sed -En 's/^Content-Type: (.*)/\1/p'
With awk you can add FS=": "
if the values contain spaces:
awk 'BEGIN {FS=": "}/^Content-Type/{print $2}'
The other answers use the -I
option and parse the output. It's worth noting that -I
changes the HTTP method to HEAD
. (The long opt version of -I
is --head
). Depending on the field you're after and the behaviour of the web server, this may be a distinction without a difference. Headers like Content-Length
may be different between HEAD
and GET
. Use the -X
option to force the desired HTTP method and still only see the headers as the response.
curl -sI http://ifconfig.co/json | awk -v FS=": " '/^Content-Length/{print $2}'18curl -X GET -sI http://ifconfig.co/json | awk -v FS=": " '/^Content-Length/{print $2}'302