如何链接卷曲,使一个输出成为另一个输入?
How to chain curl such that the output of one become another's input?
谁能告诉我如何将一个 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"
看来您需要使用命令替换。结果会是这样的。
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。 $() 括号(a.k.a.圆括号)中的命令在子shell中执行,然后将输出放在原始命令中。
所以curl GET将在子shell中执行,结果将传递给curl POST
您可以在下面的 link 阅读更多相关信息:
https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html
使用 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"
请记住,命令行参数中的密码是 public 主机。
谁能告诉我如何将一个 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"
看来您需要使用命令替换。结果会是这样的。
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。 $() 括号(a.k.a.圆括号)中的命令在子shell中执行,然后将输出放在原始命令中。
所以curl GET将在子shell中执行,结果将传递给curl POST
您可以在下面的 link 阅读更多相关信息: https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html
使用 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"
请记住,命令行参数中的密码是 public 主机。