So, in-order to send a POST request to gists (github), you can do something like this as seen in https://gist.github.com/caspyi...

curl --user "user" -X POST --data '{"description":"Created via API","public":"true","files":{"file1.txt":{"content":"Demo"}}' https://api.github.com/gists

But, in the above example, the file name and the content of the file are hard coded which is the part ..file1.txt":{"content":"Demo"}..

I am replacing the above part with my variable $file":{"content":"$content"} but initialize the variable, the json request must be enclosed in double quotes, which I did as

curl --user "user" -X POST --data "{\"description\":\"Created via API\",\"public\":\"true\",\"files\":{\"$file\":{\"content\":\"$content\"}}' https://api.github.com/gists

But this does not work, I get json error.

  "message": "Problems parsing JSON",
  "documentation_url": "https://developer.github.com/v3/gists/#create-a-gist"

Even, if I replaced all the escaped double-quotes with \' single quotes.

Does anyone know how to include a variable inside this json request? btw: I have used all headers such as

 -H "Content-Type: application/json; charset=UTF-8" 

and many combinations to validate the request but to no avail


This is what the entire content looks like.

function gist_controller(){
    content=$(cat $1)
    DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
    read -p "enter your password - " pass 
    gist_content=$(cat $1)
    curl --user "samserayo"  -H "Content-Type: application/json; charset=UTF-8" -X POST -d  "{ 'description': 'Created via API', 'public': 'true', 'files':{ ' '$1 ':{ 'content': '$gist_content'}}" https://api.github.com/gists

migrated from serverfault.com Apr 18 '16 at 20:54

This question came from our site for system and network administrators.

  • This is almost certainly better asked on Super User, as I see no real connection to server or network administration. You can flag it and request migration if you want the question moved there. – a CVn Apr 18 '16 at 20:15
  • @MichaelKjörling Thanks, flagged it. Though, I think curl request has certainly something to do with server administration. – samayo Apr 18 '16 at 20:18
  • Problems with quotes and escaping are the most basic of all programming problems (syntax errors), the question should be migrated to SO. – Alexander Apr 18 '16 at 21:11

You don't show what value you have set for $file or $content. What do those both expand out to? It's likely one, or the other, or both, contain characters the JSON parser doesn't like. Most likely, something inside the variable expansion value needs to be further escaped or encoded before submission to the JSON parser through curl.

Have you EXPORTed the environment variables?


echo "$file $content";

to make sure they are both set as you expect them to be.

  • Sorry, updated the content. – samayo Apr 19 '16 at 6:55

You didn't specify what shell you are using, but anything within double-quotes is subject to shell filename expansion. In particular, {} has meaning to most shells. That's the reason that the example you cited used single quotes to wrap the whole thing.

Shell escapes are tricky, and sometimes it seems impossible to get what you want. Honestly, in cases like this I would write a Python script to call curl, where I know the shell isn't "helping" me out.

  • Sorry, updated the content. I am using bash shell. – samayo Apr 19 '16 at 6:55

You have to put the entire string inside double quotes ". And you need to escape quotes inside the string (not convert them into other types of quotes!).

So instead of:

"{ 'description': 'Created via API', 'public': 'true', 'files':{ ' '$1 ':{ 'content': '$gist_content'}}"

You want:

"{ \"description\": \"Created via API\", \"public\": \"true\", \"files\" : { \"$1\" : { \"content\": \"$gist_content\"}}}"

You should probably test with echo and make sure the string is exactly correct.

  • Nope, this is the first thing I did, before going crazy. It does not work like that. I always get an error as I mentioned above "message": "Problems parsing JSON", – samayo Apr 19 '16 at 11:56
  • @samayo Paste the exact message you sent. Either it's legal JSON or it isn't. (Make sure each { has a matching }.) – David Schwartz Apr 19 '16 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.