Curl "write out" value of specific header

26,548

Solution 1

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

Solution 2

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}'

Solution 3

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}'
18

curl -X GET -sI http://ifconfig.co/json | awk -v FS=": " '/^Content-Length/{print $2}'
302
Share:
26,548
Hyo Byun
Author by

Hyo Byun

Updated on November 02, 2020

Comments

  • Hyo Byun
    Hyo Byun over 3 years

    I am currently writing a bash script and I'm using curl. What I want to do is get one specific header of a response.

    Basically I want this command to work:

    curl -I -w "%{etag}" "server/some/resource"
    

    Unfortunately it seems as if the -w, --write-out option only has a set of variables it supports and can not print any header that is part of the response. Do I need to parse the curl output myself to get the ETag value or is there a way to make curl print the value of a specific header?

    Obviously something like

    curl -sSI "server/some/resource" | grep 'ETag:' | sed -r 's/.*"(.*)".*/\1/'
    

    does the trick, but it would be nicer to have curl filter the header.