Search code examples
javascriptjsongithubgist

can't POST to github v3 API


I'm trying to create a public gist via javascript. I'm not using any authentication - this is all client-side.

var gist = {
    "description": "test",
    "public": true,
    "files": {
        "test.txt": {
            "content": "contents"
        }
    }
};

$.post('https://api.github.com/gists', gist, function(data) {
});

The above code throws a 400: Bad Request - Problems parsing JSON. However, my JSON is valid. Any ideas?


Solution

  • Aha - I can't pass an object to $.post. It needs to be stringified first:

    var gist = {
        "description": "test",
        "public": true,
        "files": {
            "test.txt": {
                "content": "contents"
            }
        }
    };
    
    $.post('https://api.github.com/gists', JSON.stringify(gist), function(data) {});