curl: read headers from file

Since curl 7.55.0

Easy:

$ curl -H @header_file https://example.com

... where the header file is a plain text file with an 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 from a file. They had to be modified one bye one with -H.

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 https://example.com

Invoke the script like this:

$ sh script.sh < header_file

Starting with curl 7.55.0 it can now read headers from a file:

curl -H @filename

It's that easy now.


how about this:

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

where headers.txt looks like

Header1: bla
Header2: blupp

works in BASH.

Tags:

Curl