I'm writing a script which tries to send a Gist through Github's API. All the requests need to be in the following JSON format:
{
"description": "the description for this gist",
"public": true,
"files": {
"file1.txt": {
"content": "String file contents"
}
}
}
I'm confused by how the formatting of the "content" field should be done. I am trying to send in the "content" field a text file of code such as
if(n <= 3)
n++;
else
n--;
If I append all the lines with newlines (i.e. "n++"; -> "n++;\n") and escape other characters like backslashes and quotes, then I can send the file as a string where the JSON looks like this:
{
"description": "the description for this gist",
"public": true,
"files": {
"file1.txt": {
"content": "if(n<=3)\nn++;\nelse\nn--;"
}
}
}
, but all the indentation is lost and the Gist ends up looking like this:
if(n <= 3)
n++;
else
n--;
If I send the file as a base64 encoded string, then I get a JSON parsing error. I'm writing the JSON to a text file and using curl to send the request as below
curl --user user -X POST -H 'Content-Type application/json' -d @test.txt https://api.github.com/gists
So what options do I have to send the contents of the text file with indentation preserved? I am currently writing the script in bash, but if there is a language with parsing features designed for this case, I'd be open to using that instead.
Is there any way to send the file, preserved in its indented state, as the string literal of this JSON obeject? Am I misunderstanding the API?