3

Let's take the following example:

curl -i -X POST \
-H "Content-Type: application/json" \
-d '{"jsonrpc": "2.0", "method": "Player.Open", "params":{"item":false}}' \
http://example.com/jsonrpc

Now I want to have the boolean value of "item" be set in a shell script variable such as:

 PRIVATE=false
 read -p "Is this a private? (y/[n]) " -n 1 -r
 if [[ $REPLY =~ ^[Yy]$ ]]; then
     PRIVATE=true
 fi

And I want to pass in the value of PRIVATE to item. I have tried every which way but no luck. Can anyone shed some light?

1
  • 1
    -d '....{"item":'"$PRIVATE"'}}' Commented Aug 14, 2014 at 3:17

3 Answers 3

3

You can do it this way:

curl -i -X POST \
-H "Content-Type: application/json" \
-d '{"jsonrpc": "2.0", "method": "Player.Open", "params":{"item":'"$PRIVATE"'}}' \
http://example.com/jsonrpc
Sign up to request clarification or add additional context in comments.

Comments

1

Instead of your existing -d ... line above, you could try the following:

-d "{\"jsonrpc\": \"2.0\", \"method\": \"Player.Open\", \"params\":{\"item\":$PRIVATE}}" \

That is: when using double quote speechmarks ("), bash substitutes values for variables referenced $LIKE_THIS (not the case for single quotes you were using). The downside is that you then need to escape any double-quotes in the string itself (using the backslash, as above).

Comments

0

This abomination works too.

$ npm run script -- madrid

# script
json='{"city":"'"$1"'"}'
curl -X POST -d $json http://localhost:5678/api/v1/weather

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.