curl: read headers from file

前端 未结 5 2209
梦如初夏
梦如初夏 2020-12-24 05:56

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

5条回答
  •  佛祖请我去吃肉
    2020-12-24 06:15

    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
    

提交回复
热议问题