Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 352
Gabriel Florit Avatar asked Mar 23 '12 05:03

Gabriel Florit


1 Answers

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) {});
like image 63
Gabriel Florit Avatar answered Sep 21 '22 12:09

Gabriel Florit