Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Curl "write out" value of specific header

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.

like image 755
slosd Avatar asked Sep 20 '12 11:09

slosd


People also ask

How do you specify headers in curl?

To send an HTTP header with a Curl request, you can use the -H command-line option and pass the header name and value in "Key: Value" format. If you do not provide a value for the header, this will remove the standard header that Curl would otherwise send.

How do I print response headers in curl?

By default, curl doesn't print the response headers. It only prints the response body. To print the response headers, too, use the -i command line argument.

How do I get the header of a file using cURL?

HTTP-servers feature the command HEAD which this uses to get nothing but the header of a document. When used on an FTP or FILE file, curl displays the file size and last modification time only. -H, --header <header/@file>

What does <> mean in a curl statement?

Useful for debugging and seeing what's going on "under the hood". A line starting with '>' means "header data" sent by curl, '<' means "header data" received by curl that is hidden in normal cases, and a line starting with '*' means additional info provided by curl.

Where does curl write the received data to?

If not told otherwise, curl writes the received data to stdout. It can be instructed to instead save that data into a local file, using the -o, --output or -O, --remote-name options. If curl is given multiple URLs to transfer on the command line, it similarly needs multiple options for where to save them.

What is-W in curl command?

Write out This is one of the often forgotten little gems in the curl arsenal of command line options. --write-out or just -w for short, writes out information after a transfer has completed and it has a large range of variables that you can include in the output, variables that have been set with values and information from the transfer.


1 Answers

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}' 
like image 116
Lri Avatar answered Oct 16 '22 23:10

Lri