2

I am having trouble making a request to the uber api using the rest-client gem. I believe the issue is that I am trying to pass the required information as parameters in the url instead of data field. The example curl request has that parameters after -d, but I don't know how to implement that with rest-client. Thanks for the help.

This request works in the console:

curl -v -H "Authorization: Bearer " + session[:request_token] \
     -H "Content-Type: application/json" -X POST -d \ '{"start_latitude":"37.334381","start_longitude":"-121.89432","end_latitude":"37.77703","end_longitude":"-122.419571","product_id":"a1111c8c-c720-46c3-8534-2fcdd730040d"}' \
https://sandbox-api.uber.com/v1/requests

Request from my controller that gets an error of 406, not acceptable response.

@uber_ride = JSON.load(RestClient::Request.execute(
  :method => :post,
  :url => "https://sandbox-api.uber.com/v1/sandbox/requests/product_id=#{@uberx_id}?start_latitude=#{lat_start}&start_longitude=#{lng_start}&end_latitude=#{lat_end}&end_longitude=#{lng_end}",
  :headers => {'Authorization' => 'Bearer ' + session[:request_token], 'content_type' => 'application/json'}
))  

I tried adding an additional field for data but that give a 404 Resource Not Found error.

@uber_ride = JSON.load(RestClient::Request.execute(
  :method => :post,
  :url => "https://sandbox-api.uber.com/v1/sandbox/requests/",
  :data => {'start_latitude' => lat_start, 'start_longitude' => lng_start, 'end_latitude' => lat_end, 'end_longitude' => lng_end, 'product_id' => @uberx_id},
  :headers => {'Authorization' => 'Bearer ' + session[:request_token]},
  :content_type => :json
))  
2
  • :payload => {'start_latitude' => lat_start, 'start_longitude' => lng_start, 'end_latitude' => lat_end, 'end_longitude' => lng_end, 'product_id' => @uberx_id} instead of :data => {} should help. Commented Apr 24, 2015 at 14:55
  • @mudasobwa, thanks for the help. That helped fix part of it. I added the final request that works below as an answer.
    – tylerSF
    Commented Apr 25, 2015 at 16:34

1 Answer 1

5

I finally got it working. Thanks @mudasobwa for the recommendation of changing data => to payload =>. I also had to add single quotes around the payload object so the rest-client gem wouldn't convert it out of json. ex: '{}'. Lastly, I had to change how I specified the content-type and specify it in :headers, as :content_type, NOT 'Content-Type'. See below for the final request that is now working. It's a bit messy with the variables being .to_s, but after 5 hours this is the only call I could get working.

@uber_ride = (RestClient::Request.execute(
  :method => :post,
  :url => "https://sandbox-api.uber.com/v1/requests",
  :payload => '{"start_latitude":' + lat_start.to_s + ',"start_longitude":' + lng_start.to_s + ',"end_latitude":' + lat_end.to_s + ',"end_longitude":' + lng_end.to_s + ',"product_id":"' + @uberx_id.to_s + '"}',
  :headers => {'Authorization' => 'Bearer ' + session[:request_token], :content_type => 'application/json'}
))  

Happy to hear any feedback on how I could clean this up.

1
  • Sorry for not mentioning quotes, I typed the message in a hurry. For the string, just use ruby %{} builtin: %{"start_latitude":#{lat_start}, ...}. Commented Apr 25, 2015 at 18:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.