簡體   English   中英

無法將文件中的json替換為在bash中傳遞給curl的json數據

[英]Unable to substitute json from a file into a json data passed to curl in bash

我有一個CloudFormation腳本,其中包含一個json內容,我需要替換帖子正文中的json字段或我將進行的curl POST請求的數據。

CloudFormation文件是這樣的:

{ "AWSTemplateFormatVersion": "2010-09-09", "Description": 
...}

問題是我在下面嘗試了一些代碼,但它無法正常工作。 但是,我將CloudFormation文件的內容復制並傳遞到我的POST請求的正文中,它按預期工作。 這意味着這是一個替代或腳本問題。

CLOUD_FORMATION_FILE=/home/developer/workspace/blah/blah/infrastructure/templates/component.json
template=`cat $CLOUD_FORMATION_FILE`
echo $template
curl -d '{"template": $(echo $template)}' \
-H 'Content-Type: application/json' https://base.url.com/v1/services/component-proxy/test/stacks/test-component-proxy-component \
--cert /etc/pki/tls/certs/client.crt --key /etc/pki/tls/private/client.key

我收到錯誤:

{"error": "Invalid JSON. Expecting object: line 1 column 13 (char 13)"}

您需要使用雙引號來替換字符串中的變量。

沒有必要使用$(echo $variable) ,只需使用$variable

curl -d "{\"template\": $template}" \
-H 'Content-Type: application/json' https://base.url.com/v1/services/component-proxy/test/stacks/test-component-proxy-component \
--cert /etc/pki/tls/certs/client.crt --key /etc/pki/tls/private/client.key

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM