Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use variables inside data-raw json string in a curl command

The following script of curl command works fine with variables inside double-quoted string, but how do I use variables (e.g. ip_address and user_id) inside the --data-raw '{...}'?

#!/bin/sh

ip_address="10.0.0.1"
user_id="my-user-id"
websec_token="some_string"
version="v12"

curl -w @curl-format.txt \
    --request POST "http://example.com/$version/web" \
    --header "Authorization: Bearer $websec_token" \
    --header "Content-Type: application/json" \
    --data-raw '{
        "ipAddress": "10.0.0.1",
        "userId": "my-user-id"
    }'

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Just escape the double quotes:

--data-raw "{
    \"ipAddress\": \"$ip_address\",
    \"userId\": \"$user_id\"
}"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading