curl: read headers from file

前端 未结 5 2091
梦如初夏
梦如初夏 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:29

    As answered by @dmitry-sutyagin, if your curl is at least version 7.55.0 you can use the @ notation to read headers from a file:

    curl -H @headerfile.txt https://www.google.com/  # requires curl 7.55.0
    

    If your curl is NOT 7.55.0 or newer, there's a useful hack:

    • Use the option -K/--config , and put several -H/--header
      lines in the text file.

    For instance:

    1. curl --dump-header foo.txt https://www.google.com/
    2. If necessary, dos2unix foo.txt
    3. Convert the file to -H 'header' lines, manually or with a script:

      cat foo.txt |
        awk '$1 == "Set-Cookie:"' |
        perl -ne "chomp; next if /^\\s*\$/; if (/'/) { warn; next } print \"-H '\$_'\\n\";" |
        tee headerfile.txt
      

      This might output something like:

      -H 'Set-Cookie: 1P_JAR=2018-02-13-08; [...]'
      -H 'Set-Cookie: NID=123=n7vY1W8IDElvf [...]'
      
    4. curl --config headerfile.txt https://www.google.com/

提交回复
热议问题