在bash中使用CURLPOST使用CURLGET输出[重复]

drnojrws  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(50)

此问题已在此处有答案

Escaping characters in bash (for JSON)(13个回答)
Parsing JSON with Unix tools(46个答案)
4个月前关闭。
我有下面的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 file from above
    }
  ]
}'

如何使用POST发出第二个CURL?

pdtvr36n

pdtvr36n1#

请参阅this post,了解如何将第一个命令的输出捕获到变量中。这样使用:

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

# Assuming the $output variable is a JSON object, with a property
# called 'result', use 'jq' to extract the value of that property
result=$(jq -r '.result' <<< "$output")

# As noted above, escape the double quotes with backslashes
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中的双引号需要转义。

相关问题