Using Postmates API in a rails app with HTTParty

隐身守侯 提交于 2020-01-04 14:20:59

问题


I've been working on integrating Postmates with my ecommerce rails application for on demand deliveries. I've built a controller and view for testing purposes and configured it in my routes file (built in my dev environment). For reference, here is the documentation from Postmates: docs and here is a technical blogpost: blog

Routes file:

resources :postmates do
    member do 
        post 'get_delivery'
    end
  end

The controller:

require 'httparty'
require 'json'

class PostmatesController < ApplicationController

  def get_delivery
    api_key = **hidden**
    @customer = 'cus_K8mRn4ovuNyNKV'
    @urlstring_to_post = 'https://api.postmates.com/v1/customers/' + @customer + '/delivery_quotes'

    @result = HTTParty.post(@urlstring_to_post.to_str,
      :body => { :dropoff_address => "205 E 95th Street, New York, NY 10128", 
                 :pickup_address => "619 W 54th St, New York, NY 10019" 
                }.to_json,
      :basic_auth => { :username => api_key },
      :headers => { 'Content-Type' => 'application/json' })
  end

end

The view:

<div class="container">
  <%= form_tag url_for(:controller => 'postmates', :action => 'get_delivery'), :method => 'post' do %>
  <%= submit_tag "Get Delivery", :action => 'get_delivery', :controller => 'postmates'%>  
  <% end %>
</div>

<p>Result: <%= @result %></p>

Here is the response back from the api:

{"kind"=>"error", "code"=>"invalid_params", "params"=>{"dropoff_address"=>"This field is required.", "pickup_address"=>"This field is required."}, "message"=>"The parameters of your request were invalid."}

It seems to me that the dropoff and pickup address are not being submitted in the HTTP request. Can anyone tell me if I'm making some minor syntax error or something? The fact that I'm getting this response means that my authentication is fine and so is the url. Any ideas?

Thanks.


回答1:


It looks like you're posting JSON data to the API, while the documentation (https://postmates.com/developer/docs#basics) says you should POST the data as application/x-www-form-urlencoded

I'm not really familiar with HTTParty, but you can probably just remove the .to_json and don't specify the content-type header.




回答2:


Unrelated to your question, but you shouldn't publicly post your API key. ;)



来源:https://stackoverflow.com/questions/27850845/using-postmates-api-in-a-rails-app-with-httparty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!