使用 Bash 中的 CURL 发送 POST 请求,使用 CURL 发送 GET 请求的输出。

huangapple go评论58阅读模式
英文:

Use CURL POST using a CURL GET output in bash

问题

我有以下的GET CURL,从中获取一个XML。

curl -X 'GET'
'http://local/something/something2'
-H 'accept: application/json'
-H 'authorization: auth';

现在我想要在这个POST CURL中使用上面接收到的XML:

curl -X 'POST'
'http://something/something2'
-H 'accept: application/json'
-H 'authorization: auth'
-H 'Content-Type: application/json'
-d '{
"components": [
{
"locator": "sample",
"config": 来自上面的XML文件
}
]
}';

我如何使用POST方法进行第二个CURL请求?

英文:

I have the following GET CURL from which I get an xml.

curl -X 'GET' \
  'http://local/something/something2' \
  -H 'accept: application/json' \
  -H 'authorization: auth'

Now I want to use the previous xml received above within this POST CURL:

curl -X 'POST' \
  'http://something/something2' \
  -H 'accept: application/json' \
  -H 'authorization: auth' \
  -H 'Content-Type: application/json' \
  -d '{
  "components": [
    {
      "locator": "sample",
      "config": xml file from above
    }
  ]
}'

How can I make the second CURL with POST?

答案1

得分: 0

请参考此链接了解如何将第一个命令的输出捕获到一个变量中。使用方法如下:

output=$(curl -X 'GET' \
  'http://local/something/something2' \
  -H 'accept: application/json' \
  -H 'authorization: auth')

# 假设$output变量是一个JSON对象,具有一个名为'result'的属性,
# 使用'jq'来提取该属性的值
result=$(jq -r '.result' <<< "$output")

# 如上所述,使用反斜杠转义双引号
curl -X 'POST' \
  'http://something/something2' \
  -H 'accept: application/json' \
  -H 'authorization: auth' \
  -H 'Content-Type: application/json' \
  -d "{
    \"components\": [
        {
            \"locator\": \"sample\",
            \"config\": \"$result\"
        }
    ]
}"

请注意双引号 - 双引号必须存在,以便可以使用$output变量。因此,JSON中的双引号需要进行转义。

英文:

See this post to see how to capture the output of the first command into a variable. Use it like this:


output=$(curl -X &#39;GET&#39; \
  &#39;http://local/something/something2&#39; \
  -H &#39;accept: application/json&#39; \
  -H &#39;authorization: auth&#39;)

# Assuming the $output variable is a JSON object, with a property
# called &#39;result&#39;, use &#39;jq&#39; to extract the value of that property
result=$(jq -r &#39;.result&#39; &lt;&lt;&lt; &quot;$output&quot;)

# As noted above, escape the double quotes with backslashes
curl -X &#39;POST&#39; \
  &#39;http://something/something2&#39; \
  -H &#39;accept: application/json&#39; \
  -H &#39;authorization: auth&#39; \
  -H &#39;Content-Type: application/json&#39; \
  -d &quot;{
  \&quot;components\&quot;: [
    {
      \&quot;locator\&quot;: \&quot;sample\&quot;,
      \&quot;config\&quot;: \&quot;$result\&quot;
    }
  ]
}&quot;

Note the double quotes - double quotes must be there so $output variable can be used. As a result, the double quotes in the JSON need to be escaped.

huangapple
  • 本文由 发表于 2023年2月10日 16:59:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75408870.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定