Gabriel Florit
Gabriel Florit

Reputation: 2918

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?

Upvotes: 5

Views: 1158

Answers (1)

Gabriel Florit
Gabriel Florit

Reputation: 2918

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) {});

Upvotes: 11

Related Questions