我是shell脚本的新手,我正在尝试替换对象中的数组,但它不起作用。
我正在尝试使用动态数据有效负载发出 curl 请求。
case $name in
"test1")
VALUE='[{"test": "1"},{"test": "2"}]'
;;
"test2")
VALUE='[{"test": "1"},{"test": "3"}]'
;;
?)
echo "Error"
exit 1
;;
esac
我正在做卷曲
$(curl -s -X POST
"${BASE_URL}/pdc/config/add"
-H "authorization: Bearer ${TOKEN}"
-H "content-type: application/json"
-d '{
"data": '"${VALUE}"'
}'
--insecure
)
但这会将数组作为字符串发送。
要发送的数据应该像
{"data": [{"test": "1"},{"test": "3"}]}
但现在它正在作为
{"data": "[{"test": "1"},{"test": "3"}]"}
我该如何解决这个问题?
-d
选项应为:
-d '{"data": '"${VALUE}"'}'
它扩展到
-d {"data": [{"test": "1"},{"test": "3"}]}
对于外壳,$VALUE
(或等效地,${VALUE}
(是一个字符串。