2013-05-03 57 views

回答

2

这对我有效。

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 

结果:My test gist

+0

非常感谢:) – 2013-05-03 19:36:32

+0

有什么办法可以使用Net :: HTTP.post_form(...)来做这个调用吗? – 2014-02-14 05:40:15

0

这个宝石将为你做诡计https://github.com/defunkt/gist

为了记录,它确实需要net/https。

+0

我的问题是,我想只是为了不增加整体的依赖在增加我的一个创业板的小功能。 – 2013-05-03 18:46:25

+0

http://www.travisberry.com/2011/05/create-github-gist-with-ruby/ - 这应该做的伎俩然后。 – baordog 2013-05-03 18:50:58

+0

这是Gtihub API的指南v1 – 2013-05-03 18:53:15