Tags

Share it

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!

Share it now!
If you enjoyed this post, make sure you subscribe to my RSS feed!