1
So, let's say I have an index.php
file containing this:
<?= "Hello" ?>
<?= echo "WORLD" ?>
And I wanted upload the contents of this file to my gists in github, which I am doing via
gist_content=$(cat 'index.php')
curl --user "GITHUB_USER" -H "Content-Type: application/json; charset=UTF-8" -X POST -d "{ \"description\": \"Created via API\", \"public\": \"true\", \"files\":{ \"index.php \":{ \"content\": \"$gist_content\"}}\" " https://api.github.com/gists
Now, this script does not work for some reason, and I get error response
{
"message": "Problems parsing JSON",
"documentation_url": "https://developer.github.com/v3/gists/#create-a-gist"
}
If I write everything in one line without tags, quotes like hello
it works find
If I started the JSON request with single quotes, then
$gist_content
won't be evaluated. – samayo – 2016-05-13T14:57:54.933When you use with double-quotes you need to escape it, but your escaping is not correct. Let me update the post, but my computer is bloody slow. You need to do like:
" "\"" "
to escape a single double-quote. Secondly your JSON has uneven number brackets, or something. – kenorb – 2016-05-13T15:03:30.860I am saying, if I use single quotes the bash $variable won't be read. You can try and then let me know, if it works for you – samayo – 2016-05-13T15:13:38.917
@samayo I know, I'm just saying you've the syntax error in your JSON, so you can do the rest. I just simplified the example for you to show you what is the valid JSON format and I've explained where is the issue, so you can fix it by yourself by understanding the problem. You've just a typo, that's all. – kenorb – 2016-05-13T15:14:30.337
like I said, your final example gives me the same error I posted in the question. It is a tricky situation. – samayo – 2016-05-13T15:22:53.983
You also need to escape your file content as well. Or use single quotes inside the
index.php
. – kenorb – 2016-05-13T16:16:48.820Check out the first block of code in my question, the php code does not need escaping as it contains no error, so is there anyway to escape the content automatically with bash? – samayo – 2016-05-13T16:26:30.553
PHP has double-quotes which you're injecting into your JSON, so they're in conflict with other double-quotes. So PHP's double-quotes needs escaping, as well as new lines. Updated. – kenorb – 2016-05-13T16:46:04.387
+1
for the effort, but I get the same error despite your update. The problem is not as easy as it seems. – samayo – 2016-05-13T16:59:31.350It works for me. You should update your question with relevant details, if still doesn't work. – kenorb – 2016-05-13T20:46:13.027
You mean you were able to send the gist with the contents from the index.php file? – samayo – 2016-05-13T21:12:03.883
I didn't have any syntax error which you've mentioned as it generated JSON format correctly (in comparison to yours which had a syntax error), but I haven't tested gist request, since I've 2-factor auth, so I couldn't fully test it. Although your original code included in your question where you've syntax error is now corrected. – kenorb – 2016-05-13T21:24:40.133