Search code examples
javascriptjsonajaxxmlhttprequestgist

How do I update a Gist?


I want to update my Gist from another website, where I log in with my gist token. I can't get it to work. I managed to get a gist via GET, but updating a gist with PATCH isn't working.

I don't think it's a problem with authentication, because when getting a gist, my username and profile showed up correctly.

JavaScript (JQuery):

$.ajax({ 
  url: 'https://api.github.com/gists/e3e0b182c09bf333593c',
  type: 'PATCH',
  beforeSend: function(xhr) { 
    xhr.setRequestHeader("Authorization","token f32e-----MY-TOKEN-(GIST-ACCESS)-----6f44"); 
  }, data: {
    "description":"Edit gist",
    "files":{
      "annexation.json":{
        "content":"{\"updated content\":\"from Ajax\"}"
      }
    }
  }
}).done(function(response) {
  $('#write').text(JSON.stringify(response));
});

I keep getting an Error 400 (Bad Request).

Response:

{
  "message": "Problems parsing JSON",
  "documentation_url": "https://developer.github.com/v3/gists/#edit-a-gist"
}

Can someone point out if I'm doing something wrong? Thanks a lot.


Solution

  • Alright, after some fiddling this has been the problem the entire time:

    data should be a string, not an object.

    data: '{"description":"Edit gist","files":{"annexation.json":{"content":"{\"updated content\":\"from Ajax\"}"}}'