Ask A Question

Notifications

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 = File.read("#{Rails.root}/private/websummit.pem")
uri = URI.parse(apipath)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.cert = OpenSSL::X509::Certificate.new(pem)
http.key = OpenSSL::PKey::RSA.new(pem, ENV['KEY_PASSWORD'])
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.start {
if format == 'get'
request = Net::HTTP::Get.new(apipath)
elsif format == 'put'
request = Net::HTTP::Put.new(apipath)
end
http.request(request) {|res|
respond_to do |format|
result = { :message => res.body }
format.json { render :json => result }
end
}
}
end

I can pass it an appropriate URL such as:

callSmart('https://api.prod.smartservices.car2go.com/vega/vehicles/CAR01?fields=connection.connected','get')

and get the result:

{"message":"{\"connection\":{\"connected\":true}}"}

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

callSmart('https://api.prod.smartservices.car2go.com/vega/vehicles/CAR01/blink','put')

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?

Reply

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. =(

Reply

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.

Reply

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 https://github.com/rest-client/rest-client and go to town.

Reply
Join the discussion
Create an account Log in

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

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

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