Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a shell variable to a JSON request to curl?

Tags:

json

bash

curl

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?

like image 403
iOSGuy Avatar asked Feb 05 '26 10:02

iOSGuy


1 Answers

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
like image 164
Wenbing Li Avatar answered Feb 07 '26 03:02

Wenbing Li



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!