Can't update a gist immediately after creating it

165 views Asked by At

I am trying to use the gist API as a adhoc diff tool. To do this I want to upload some text to a gist and then update it and view the diff. For some reason I am getting "{\"message\":\"Not Found\",\"documentation_url\":\"https://developer.github.com/v3\"}" as a result. Its slightly confusing as when I view the url I can see the GIST fine.

My code is as follows

uri = URI("https://api.github.com/gists")

first_payload = {
    'description' => "My test gist",
    'public' => true,
    'files' => {
        'change.yml' => {
            'content' => "before"
        }
    }
}

req = Net::HTTP::Post.new(uri.path)
req.body = first_payload.to_json
req.basic_auth("username", "password")
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) do |http|
  http.request(req)
end

second_uri = URI(JSON.parse(res.body)["url"])
second_payload = {
    'description' => "My test gist",
    'public' => true,
    'files' => {
        'change.yml' => {
            'content' => ':after'
        }
    }
}

second_req = Net::HTTP::Put.new(second_uri.path)
second_req.body = second_payload.to_json
second_req.basic_auth("username", "password")
second_res = Net::HTTP.start(second_uri.hostname, second_uri.port, :use_ssl => true) do |http|
  http.request(second_req)
end
1

There are 1 answers

0
Donovan Thomson On

second_req = Net::HTTP::Patch.new(second_uri.path) did the trick