1

I am having issues with sending the proper json data in a curl -X POST command. I have successfully run the POST command locally on my mac by copy and pasting the hardcoded values in but I am now trying to create a .sh script to automate this process. Upon running the code below I get this error:

{"message":"Unexpected end of JSON input"}

Here is the output json from JSON_STRING with names made generic and nothing else changed:

{ "url": "api_url", "tileset": "username.filename" }

Once I can figure out how to properly format the json in the POST command I know it will work, but I can't seem to get the syntax right. Hoping a set of fresh/experience bash eyes will be able to catch my mistake:). Also, all variables that I have are correct and already been confirmed by running variable values in mac terminal. Thanks in advance for the help!

    curl_url="http://${bucket}.s3.amazonaws.com/${key}"
    echo $curl_url

    tileset_id="username.filename"

    JSON_STRING=$(jq -n \
                  --arg bn "$curl_url" \
                  --arg on "$tileset_id" \
                  '{url: $bn, tileset: $on}')

    echo $JSON_STRING

    curl -X POST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d $JSON_STRING 'apiurl'
304techmaven
  • 166
  • 1
  • 11
  • print out the generated json strinng and share it# – Jens Mar 20 '20 at 17:23
  • @Jens here is the generated json string `{ "url": "api_url", "tileset": "username.filename" }` I changed the api_url, usersname, and filename values to be generic but didn't mess with any spacing etc. – 304techmaven Mar 20 '20 at 17:30
  • Can you please add it to the question not as comment – Jens Mar 20 '20 at 17:31
  • What is the ` 'apiurl'` at the end? I am not sure bit i think it will be used as part of the json string by curl – Jens Mar 20 '20 at 17:31
  • Just updated the question. Sorry I am still learning the ropes of stack overflow. That is the url for the api that I am trying to POST to. I substituted it out because it had access tokens in it etc. – 304techmaven Mar 20 '20 at 17:33
  • try to put it in front of `-d`. – Jens Mar 20 '20 at 17:35
  • put the api url or the JSON_STRING variable in front of the -d? – 304techmaven Mar 20 '20 at 17:38
  • http://shellcheck.net/ would identify your problem automatically. See the wiki page for the relevant warning it generates, [SC2086](https://github.com/koalaman/shellcheck/wiki/SC2086). – Charles Duffy Mar 20 '20 at 18:09
  • (btw, why is `JSON_STRING` all-caps? POSIX reserves all-caps names for variables that reflect or modify behavior of the shell or POSIX-specified tools, leaving lowercase names available for application use; see https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html, fourth paragraph, keeping in mind that setting a regular shell variable will implicitly modify any like-named environment variable, so conventions necessarily apply to both types). – Charles Duffy Mar 20 '20 at 18:11
  • Folks, thanks for all the comments! @glenn jackman answer was a quick fix and allowed me to successfully make the POST call. – 304techmaven Mar 20 '20 at 20:42

1 Answers1

2

Absolutely required to quote the shell variable:

curl ... -d "$JSON_STRING" http://example.com/end/point

Otherwise, the shell will do word splitting, and the argument to -d becomes just {"url":


as a side note, bash arrays can help with the readability:

curl_url="http://${bucket}.s3.amazonaws.com/${key}"
tileset_id="username.filename"

JSON_STRING=$(
    jq -n \
          --arg bn "$curl_url" \
          --arg on "$tileset_id" \
          '{url: $bn, tileset: $on}'
)

curl_opts=(
    -X POST
    -H "Content-Type: application/json" 
    -H "Cache-Control: no-cache" 
    -d "$JSON_STRING"
)

curl "${curl_opts[@]}" 'apiurl'
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • This worked perfect! Thanks for the help glenn, appreciate the bash array example as well, will def use that in the future – 304techmaven Mar 20 '20 at 20:42
  • Have a read through [Security implications of forgetting to quote a variable in bash/POSIX shells](https://unix.stackexchange.com/q/171346/4667) to see why this is so important. Follow the links in the question there. – glenn jackman Mar 20 '20 at 22:06