Ruby HTTP Post with JSON Request + SSL + Certificate + Basic Authentication

Published: March 3, 2012

Antano Solar John is the creator and developer of the most advanced technologies to design and fast-track personal evolution and an investor in startups that have access to cutting-edge solutions for personal excellence.

Spent an easy three hours on a laggy internet connection (Gem is broken in my Mac. And was updating Xcode.) working on a remote server across the continent trying to figure how to use Ruby to make secure post requests that are in the JSON format uses SSL with a certificate and basic authentication in the header.

I started with rest_client, realized it couldn’t do the certificate part for whatever reasons. (There was a bug. I wish I had logged in)

And finally after slow progressive improvements. Here is the code:

require 'net/http'
require 'uri'
uri = URI.parse(@url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
http.ca_file = "equifax_ca.crt"

request = Net::HTTP::Post.new(uri.request_uri,initheader = {‘Content-Type’ =>’application/json’})
request.body = payload.to_json #payload is defined earlier as a hash with key/value pair

request.basic_auth @username, @apikey
response = http.request(request)
return response.body

Hope you find this early when you need it :)

Cheers!

Related Posts

Rails on Lion

Rails on Lion

Tools & Showcase

Tools & Showcase

5 Min Mentor – Quick and Measurable Learning by Content Free Diagnosis

5 Min Mentor – Quick and Measurable Learning by Content Free Diagnosis

Shell for Catalyst

Shell for Catalyst

The Antano Solar Voice  is one of the most cutting-edge blog and podcast in the world with fresh insights from validated life experience. The Solar Voice season 1 podcast has been ranked #3 in Apple iTunes News & Noteworthy. And the posts have been published in over a dozen publications including Business World, YourStory, Hindu Business Line, Economic Times and  many more. To listen to any of the past episodes for free, check out this page. And to read the past article, check out the blog page.

Leave a Reply


Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}