英文:
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 '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\"
}
]
}"
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论