【问题标题】:How to chain curl such that the output of one become another's input?如何链接 curl 以使一个输出成为另一个输入?
【发布时间】:2021-06-15 15:50:18
【问题描述】:

谁能告诉我如何将一个 CURL GET 的输出传递给另一个 CURL POST?我的意思是这样的:

curl --header "Content-Type: application/json" --request POST --data "{\"specification\": curl --header "Content-Type: application/json" --request GET "https://user:pass@anotherhost"}" "https://user:pass@localhost"

【问题讨论】:

    标签: linux curl continuous-integration


    【解决方案1】:

    使用 jq 在 shell 中制作 JSON。

    response="$(curl --header "Content-Type: application/json" --request GET "https://user:pass@anotherhost")"
    data="$(jq "{specification: .}" <<< "$response")"
    curl --header "Content-Type: application/json" --request POST --data "$data" "https://user:pass@localhost"
    

    请记住,命令行参数中的密码对主机是公开的。

    【讨论】:

    • 非常感谢!
    【解决方案2】:

    看来您需要使用命令替换。结果会是这样。

    curl --header "Content-Type: application/json" --request POST --data "{\"specification\": $(curl --header "Content-Type: application/json" --request GET "https://user:pass@anotherhost")}" "https://user:pass@localhost"
    

    $() 用于命令替换,它调用一个子shell。 $() 括号中的命令(也称为圆括号)在子shell中执行,然后将输出放在原始命令中。

    所以 curl GET 将在子 shell 中执行,结果将传递给 curl POST

    您可以通过以下链接了解更多信息: https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-20
      • 1970-01-01
      • 2016-10-18
      相关资源
      最近更新 更多