代码之家  ›  专栏  ›  技术社区  ›  JackTheKnife

cURL获取文件中的请求和参数

  •  0
  • JackTheKnife  · 技术社区  · 6 年前

    有没有办法在文件中包含参数,而不是像这样

    curl -X GET -G 'http://rest_url' -d 'param1=value1' -d 'param2=value2' -d 'paramX=valueX'
    

    curl -X GET -G 'http://rest_url' -d @file.txt
    

    我已转移到该文件

    'param1=value1' 
    'param2=value2' 
    'paramX=valueX'
    

    但这行不通。

    0 回复  |  直到 6 年前
        1
  •  2
  •   Daniel Stenberg    6 年前

    对您可以在文件中执行这些操作,但它们将显示为单d选项,因此curl不会在参数之间插入与,因此您必须编写文件以包含如下数据:

    param1=value1&param2=value2
    
    curl -G 'http://rest_url' -d @file.txt
    

    如果您希望像问题中那样用换行符分隔参数,我建议使用一个小的shell脚本来解决这个问题:

    #!/bin/sh
    while read param; do
      args="$args -d $param";
    done
    curl -G $args http://example.com
    

    使用方法如下:

    script.sh < file.txt