[英]echo contents of file into a curl command
I am trying to run a curl command something like below:我正在尝试运行 curl 命令,如下所示:
curl -X POST "https://$CLUSTER/api/internal/vmware/vm/snapshot/$snapshot_id/restore_files" -H "accept: application/json" -H "authorization: "$authType" "$hash_password"" -H "Content-Type: application/json" -d "{\"destObjectId\": \"$vm_id\", \"restoreConfig\": [ echo $(cat suhas_file.txt) ], \"shouldUseAgent\": true, \"shouldRestoreXAttrs\": true}" -k -s
I get Request Malformed
我收到
Request Malformed
Due to the echo $(cat suhas_file.txt)
由于
echo $(cat suhas_file.txt)
If I run this echo command manually I get:如果我手动运行这个 echo 命令,我会得到:
echo $(cat suhas_file.txt)
{\"path\": \"C:\\\\bootmgr\"},{\"path\": \"C:\\\\Program Files\\\\desktop.ini\"}
If I paste this manually instead of echo in the above curl like如果我在上面的 curl 中手动粘贴而不是 echo
curl -X POST "https://$CLUSTER/api/internal/vmware/vm/snapshot/$snapshot_id/restore_files" -H "accept: application/json" -H "authorization: "$authType" "$hash_password"" -H "Content-Type: application/json" -d "{\"destObjectId\": \"$vm_id\", \"restoreConfig\": [ {\"path\": \"C:\\\\bootmgr\"},{\"path\": \"C:\\\\Program Files\\\\desktop.ini\"} ], \"shouldUseAgent\": true, \"shouldRestoreXAttrs\": true}" -k -s
It works.有用。
I tried我试过了
value=`cat suhas_file.txt`
And then echo $value
inside the curl and I still get malformed request.然后在 curl 中
echo $value
,我仍然收到格式错误的请求。
How can i basically echo the contents of this file that we see above within the []
of curl我怎样才能在 curl 的
[]
中基本上回显我们在上面看到的这个文件的内容
Try:尝试:
printf -v data '
{
"destObjectId": "%s",
"restoreConfig": [%s],
"shouldUseAgent": true,
"shouldRestoreXAttrs": true
}
' "$vm_id" "$(< suhas_file.txt)"
curl -X POST \
-H "accept: application/json" \
-H "authorization: $authType $hash_password"" \
-H "Content-Type: application/json" \
-d "$data" \
-k -s \
"https://$CLUSTER/api/internal/vmware/vm/snapshot/$snapshot_id/restore_files"
Added line continuations for readability.为可读性添加了续行。
In the suhas_file.txt
file, do not escape the double quotes -- it should be plain JSON data.在
suhas_file.txt
文件中,不要转义双引号——它应该是普通的 JSON 数据。
$(< filename)
is a bash construct, equivalent to $(cat filename)
. $(< filename)
是一个 bash 结构,相当于$(cat filename)
。 See 3.5.4 Command Substitution in the manual参见手册中的3.5.4 命令替换
To safely compose a dynamic JSON data-set from a shell script, a JSON-aware parser like jq
will ensure proper formatting.为了从 shell 脚本安全地组合动态 JSON 数据集,像
jq
这样的 JSON 感知解析器将确保正确格式化。
Example:例子:
#!/usr/bin/env sh
authType='authTypeBasic'
hash_password='samplePassword'
snapshot_id='snapshot42'
vm_id='Marvin'
data="$(
jq \
--null-input \
--compact-output \
--arg VMID "$vm_id" \
--rawfile restoreConfig suhas_file.txt \
'{
"destObjectId": $VMID,
"restoreConfig": [ $restoreConfig | split("\n") ],
"shouldUseAgent": true,
"shouldRestoreXAttrs": true
}'
)"
curl \
--request 'POST' \
--header "Accept: application/json" \
--header "Authorization: $authType $hash_password" \
--header "Content-Type: application/json" \
--data "$data" \
--insecure \
--silent \
--url "https://$CLUSTER/api/internal/vmware/vm/snapshot/$snapshot_id/restore_files"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.