简体   繁体   中英

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

I have a CloudFormation script that contains a json content that I need to substitute for a json field within the post body or data of the curl POST request that I will make.

The CloudFormation file is like this:

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

The problem is that I have tried some code below but it is not working. However, I copy and past the content of the CloudFormation file into my POST request's body it works as expected. This implies that this is a substitution or scripting problem.

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

I am getting the error:

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

You need to use double quotes in order to substitute a variable in a string.

There's no need to use $(echo $variable) , just use $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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM