Curl "write out" value of specific header Curl "write out" value of specific header shell shell

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 variables specified for "-w" are not directly connected to the http header.So it looks like you have to "parse" them on your own:

curl -I "server/some/resource" | grep -Fi etag


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