Search code examples
jqueryajaxbackbone.jsgithubgist

How do I update a Gist via the GitHub API?


For the past couple hours I've been trying to update a GitHub Gist via their API to no avail. I can easily POST to https://api.github.com/gists and create a new Gist, but I can't ever get a PATCH to https://api.github.com/gists/:id to work. How do I update Gists via their API? Am I missing some important detail?

Here's a JSFiddle of my present predicament: http://jsfiddle.net/ZzUsv/4/

The code itself:

var Gist = Backbone.Model.extend({
    urlRoot: 'https://api.github.com/gists',
    defaults: {
        description: 'A terse gist',
        'public': true,
        files: {
            'html.html': {
                content: 'test'
            },
            'css.css': {
                content: 'test'
            },
            'js.js': {
                content: 'test'
            }
        }
    }
});

var my_gist = new Gist;
my_gist.save( my_gist.toJSON(), {
    success: function(){
        $('body').append('successfully created gist');
        my_gist.save({ description: '<div>A less terse gist</div>' }, {
            patch: true,
            success: function(){
                $('body').append('<div>successfully updated gist</div>');
            },
            error: function( model, xhr, options ){
                $('body').append('<div><b>error updating gist</b></div>');
                $('body').append( JSON.stringify( xhr ) );
            }
        });
    }
});

Relevant documentation: http://developer.github.com/v3/gists/#edit-a-gist


Solution

  • Looks like this is a simple case of poor documentation. It seems you're not able to edit or delete anonymous Gists, despite having the ID provided to you.