Thursday, September 1, 2022

[SOLVED] curl: read headers from file

Issue

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.


Solution

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


Answered By - Daniel Stenberg
Answer Checked By - Pedro (WPSolving Volunteer)