Ruby HTTPS POST’ing

So for my own geeky pleasure, I decided to try writing cgi scripts with Ruby, Python, PHP, and Perl. All had readily accessible documentation on how to POST to a https URL but ruby. My first thought was to look at the Net:HTTP documentation found HERE.

The one example I wanted was not listed. I did some searching around and pieced together the following code. I hope this is as big a help to you as it was to me. Looking at it, It seems pretty intuitive….but if your like me, sometimes you need it spelled out 😀

As a side note, setting path to path = '/../' is my work-around for a script that is mapped to www.mysite.com rather than '/some_POST_handling_script.rb'

#Juan Vazquez ->javazquez.com
require 'net/http'
require 'net/https'
http = Net::HTTP.new('www.mysite.com', 443)
http.use_ssl = true
#path(a.k.a) ->www.mysite.com/some_POST_handling_script.rb'
path = '/some_POST_handling_script.rb'
data = 'badguy=Gargamel'

headers = {'Content-Type'=> 'application/x-www-form-urlencoded'}

resp, data = http.post(path, data, headers)

puts 'Code = ' + resp.code
puts 'Message = ' + resp.message
resp.each {|key, val| puts key + ' = ' + val}
puts data