0

I have a script like this:

#!/bin/bash
curl --request POST --url http:/myUrl.com/etc --header 'content-type: application/json' --data '{"myId": $1, "services": {"ENABLE_THE_SERVICE": "1"}}';

Then when I try to run: ./myScript.sh 77777 there is an error like:

{"code":"BAD_REQUEST","message":"[line: 1, column: 17] Unexpected character ('$' (code 36)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: org.jboss.resteasy.core.interception.MessageBodyReaderContextImpl$InputStreamWrapper@45771071; line: 1, column: 17]"}

Do you know how to make it work? thanks..

1 Answer 1

1

Don't single-quote the string that contains the $1, that will stop the shell from expanding it.

Instead:

curl --request POST \
    --url 'http:/myUrl.com/etc' \
    --header 'content-type: application/json' \
    --data '{"myId": '"$1"', "services": {"ENABLE_THE_SERVICE": "1"}}'

This ends the first part of the data string just before the $1, then double quotes the $1 itself, and concatenates that with a new single-quoted string with the rest of the data content.

This assumes that the string in $1 is already a JSON-encoded string. If it is not, you may want to use jq to construct the data payload:

curl --request POST \
    --url 'http:/myUrl.com/etc' \
    --header 'content-type: application/json' \
    --data "$( jq -nc --arg id "$1" '{"myId": $id, "services": {"ENABLE_THE_SERVICE": "1"}}' )"
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .