Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

curl: read headers from file

Tags:

curl

After the --dump-header writes a file, how to read those headers back into the next request? I would like to read them from a file because there are a number of them.

I tried standard in: cat headers | curl -v -H - ...

I'm actually using the feature in Firebug to "Copy Request Headers" and then saving those to a file. This appears to be the same format.

like image 820
jcalfee314 Avatar asked Sep 30 '12 12:09

jcalfee314


People also ask

How do I get the header info from curl?

We can use curl -v or curl -verbose to display the request headers and response headers in the cURL command. The > lines are request headers .

What is in curl request?

'cURL' is a command-line tool that lets you transmit HTTP requests and receive responses from the command line or a shell script. It is available for Linux distributions, Mac OS X, and Windows. To use cURL to run your REST web API call, use the cURL command syntax to construct the command.

What headers does curl send by default?

Curl by default adds headers such as Content-type and User-agent .


2 Answers

since curl 7.55.0

Easy:

$ curl -H @header_file $URL 

... where the header file is a plain text file with a HTTP header on each line. Like this:

Color: red Shoesize: 11 Secret: yes User-Agent: foobar/3000 Name: "Joe Smith" 

before curl 7.55.0

curl had no way to "bulk change" headers like that, not even from a file.

Your best approach with an old curl version is probably to instead write a shell script that gathers all the headers from the file and use them, like:

#!/bin/sh while read line; do   args="$args -H '$line'"; done curl $args $URL 

Invoke the script like this:

$ sh script.sh < header_file 
like image 173
Daniel Stenberg Avatar answered Sep 28 '22 00:09

Daniel Stenberg


how about this:

curl -v -H "$(cat headers.txt)" yourhost.com 

where headers.txt looks like

Header1: bla Header2: blupp 

works in BASH.

like image 35
Cpt. Senkfuss Avatar answered Sep 28 '22 00:09

Cpt. Senkfuss