Reputation: 5818
I am writing a Bash script to construct arguments for a curl command to interface with the API.
The following command works fine, even when running from my bash script:
curl "https://www.site.com/api" -H "username:user" -H "token:3028c67e929221bb9993847eba6bc038" -H "Content-Type:undefined" -X POST --data '{"user_id":"me","name":"a","subject_id":"a","build":"hg18","file_format":"vcf","notifyByEmail":"Y"}'
So I'm trying to build the JSON component, using the following Bash Script:
ANNOTATION_INIT_NAME="GELG00000000003"
ANNOTATION_INIT_SUBJECT="GELG00000000003"
ANNOTATION_INIT_PROJECT="4"
ANNOTATION_INIT_BUILD="hg19"
ANNOTATION_INIT_FAMILY="0"
ANNOTATION_INIT_DATA="'{\"user_id\":\"me\",\"name\":\"$ANNOTATION_INIT_NAME\",\"subject_id\":\"$ANNOTATION_INIT_SUBJECT\",\"project\":\"$ANNOTATION_INIT_PROJECT\",\"build\":\"$ANNOTATION_INIT_BUILD\",\"family_id\":\"$ANNOTATION_INIT_FAMILY\",\"file_format\":\"vcf\",\"notifyByEmail\":\"N\"}'"
echo $ANNOTATION_INIT_DATA
ANNOTATION_INIT=$(curl "https://www.site.com/api" $AUTH_HEADERS -H "Content-Type:undefined" -X POST --data $ANNOTATION_INIT_DATA)
Echoing $ANNOTATION_INIT_DATA
reveals exactly what I'm looking for:
'{"user_id":"me","name":"","subject_id":"GELG00000000003","project":"4","build":"hg19","family_id":"0","file_format":"vcf","notifyByEmail":"N"}'
However the ultimate call returns an error:
{
"error": "Missing required field, user_id."
}
Is there anything obviously wrong with my approach, or even better, a way I can output commands as I run them verbatim to know exactly how my command is constructed?
Upvotes: 0
Views: 214
Reputation: 7981
You need to quote $ANNOTATION_INIT_DATA
:
ANNOTATION_INIT=$(curl "https://www.site.com/api" $AUTH_HEADERS -H "Content-Type:undefined" -X POST --data "$ANNOTATION_INIT_DATA")
Otherwise the spaces inside the variable will be interpreted as additional arguments to curl.
Edit: Also, as @jonathan-leffler notes below: 'the single quotes embedded in ANNOTATION_INIT_DATA
are not wanted. They're needed when you type the argument literally, but not when it is written as --data "$ANNOTATION_INIT_DATA"
'
Upvotes: 4