Watermelon
Watermelon

Reputation: 84

Shell script not working for curl request

I want to make a curl request with Databox to push somemetrix and want to do it in shell script. Here is the databox POST request example (which works like a charm)

curl https://push.test \
-u token
: \
-X POST \
-H 'Content-Type: application/json' \
-H 'Accept: application/vnd.databox.v2+json' \
-d '{
  "data":[
    {
      "$testcount": 50,
      "test_name": "test"
    }
  ]
}'

When I form the json body as a separate json string and try to pass as parameter, it doesn't work and gives a json parsing error. I am not sure what am I am doing wrong here. can someone help? I am new to shell scripts

#!/bin/bash
JSON_STRING= '{"data" : [{"$testcount":50,"testname":"test"}]}'
echo    "$JSON_STRING" 

curl https://testpush \
-u token
: \
-X POST \
-H 'Content-Type: application/json' \
-H 'Accept: application/vnd.databox.v2+json' \
-d '$JSON_STRING'
    

error : {"type":"invalid_json","message":"Invalid request body - JSON parse error"}

I have added my token for the request, so the authorisation should work.

Upvotes: 0

Views: 909

Answers (1)

user2849202
user2849202

Reputation:

You have excess whitespace around the =.

Also, $JSON_STRING in the last line of the second script should be in double quotes instead of the single quotes, to get it expanded into what you just set it to.

Btw., if data gets out of hand or is sensitive, you might want to look into the possibility to start the data with the letter @ and then have the rest be a file name that contains the data.

Upvotes: 1

Related Questions