Posting a gist to github.com in ruby not working
I am trying to create a new gist on github.com by posting the the URL. I have done this in C# and it works fine, but when I try to replicate in ruby on rails the post never seems to work I am always just redirected to the gists/new URL which indicates that the post was not accepted. I figure I am just missing something fundamental in ruby.
require 'net/https'
require 'open-uri'
url = URI.parse('https://gist.github.com/gists')
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = tru开发者_开发技巧e
req = Net::HTTP::Post.new(url.path)
req.form_data = "file_name[gistfile1]=testclip.txt&description=Test Clip&file_contents[gistfile1]=This is my test clip&login=uname&token=secret"
http.start{|h| h.request(req)}['Location']
I'm out on a limb here, but I'm guessing it has to do with the SSL verify mode. You'll either need to keep from verifying:
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
or give it something to verify against:
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
http.cert = OpenSSL::X509::Certificate.new(ca_cert)
Take a look at the ca_cert
method at Defunkt's Gist gem for more info (you'll need a bundle).
精彩评论