Ask A Question


You’re not receiving notifications from this thread.

Having trouble figuring out net/http and API calls.

Seona Bellamy asked in Rails

Hi folks,

I need to make a number of calls to an API, some of which are GET requests and some of which are PUT requests. I've tried to make a common function since there's a bunch of security stuff that needs to happen, and I'm not sure if a) I'm actually doing it right in the first place, or b) there's a better/simpler/cleaner/more reliable way to do it instead.

I have the following function to handle the contact with the Smart API:

def callSmart(apipath,format)
pem ="#{Rails.root}/private/websummit.pem")
uri = URI.parse(apipath)
http =, uri.port)
http.use_ssl = true
http.cert =
http.key =, ENV['KEY_PASSWORD'])
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.start {
if format == 'get'
request =
elsif format == 'put'
request =
http.request(request) {|res|
respond_to do |format|
result = { :message => res.body }
format.json { render :json => result }

I can pass it an appropriate URL such as:


and get the result:


So far, so good. However I then tried calling one of the actions on the car, rather than passively requesting data:


This got me the following response:

{"message":"{\"message\":\"Service is not authenticated\"}"}

Given that it's running the same authentication code, do you have any idea why one is working and the other not?


I know it seems simple... but have you verified that you actually have put priviledges from the provider?

I'm dealing with this right now on a project where I have access to everything except for one resource, so I'm in a holding pattern until they fix the account. The error message returned was just null so I burned a day thinking there was something wrong with the connection method / query. =(


It works on a straight curl request, so I'm figuring we have the right priviledges unless curl works differently - I'm not really that familiar with it.


Hmm, well I don't see anything that jumps out that would be wrong with your setup, but I've never really used the Net::HTTP API so I'm not much help there.

Have you by chance tried using one of the rest gems just to make sure it's not a setup issue? When I need REST support I drop in and go to town.

Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 78,890+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2023 GoRails, LLC. All rights reserved.