2

Is there a way to create an Anonymous (public or private) gist using net/http or net/https?

2 Answers 2

4

This worked for me.

require 'net/http'
require 'json'

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

payload = {
  'description' => "My test gist",
  'public' => true,
  'files' => {
    'test.txt' => {
      'content' => "This is a test!\n\nI am making a public gist."
    }
  }
}

req = Net::HTTP::Post.new(uri.path)
req.body = payload.to_json

puts req.inspect
puts req.body.inspect

# GitHub API is strictly via HTTPS, so SSL is mandatory
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) do |http|
  http.request(req)
end

puts res.inspect
puts res.body.inspect

Result: My test gist

Sign up to request clarification or add additional context in comments.

1 Comment

Is there any way to do this call using Net::HTTP.post_form(...) ?
0

This gem will do the trick for you https://github.com/defunkt/gist!

For the record, it does require net/https.

3 Comments

My issue is that I want just to add a little functionality in a gem of mine without adding a whole dependency.
This is a guide for Gtihub api v1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.