在带有 HTTParty 的 rails 应用中使用 Postmates API
Using Postmates API in a rails app with HTTParty
我一直致力于将 Postmates 与我的电子商务 rails 应用程序集成以实现按需交付。我已经为测试目的构建了一个控制器和视图,并在我的路由文件中配置了它(在我的开发环境中构建)。作为参考,这里是 Postmates 的文档:docs and here is a technical blogpost: blog
路线文件:
resources :postmates do
member do
post 'get_delivery'
end
end
控制器:
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
观点:
<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>
这是从 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."}
在我看来,在 HTTP 请求中没有提交送货地址和取货地址。谁能告诉我我是不是犯了一些小的语法错误之类的?我收到此响应的事实意味着我的身份验证没有问题,url 也没有问题。有什么想法吗?
谢谢。
看起来您正在将 JSON 数据发布到 API,而文档 (https://postmates.com/developer/docs#basics) 说您应该 POST 将数据作为 [=10] =]
我对 HTTParty 不是很熟悉,但是你可以只删除 .to_json
而不要指定 content-type header.
与您的问题无关,但您不应公开 post 您的 API 密钥。 ;)
我一直致力于将 Postmates 与我的电子商务 rails 应用程序集成以实现按需交付。我已经为测试目的构建了一个控制器和视图,并在我的路由文件中配置了它(在我的开发环境中构建)。作为参考,这里是 Postmates 的文档:docs and here is a technical blogpost: blog
路线文件:
resources :postmates do
member do
post 'get_delivery'
end
end
控制器:
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
观点:
<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>
这是从 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."}
在我看来,在 HTTP 请求中没有提交送货地址和取货地址。谁能告诉我我是不是犯了一些小的语法错误之类的?我收到此响应的事实意味着我的身份验证没有问题,url 也没有问题。有什么想法吗?
谢谢。
看起来您正在将 JSON 数据发布到 API,而文档 (https://postmates.com/developer/docs#basics) 说您应该 POST 将数据作为 [=10] =]
我对 HTTParty 不是很熟悉,但是你可以只删除 .to_json
而不要指定 content-type header.
与您的问题无关,但您不应公开 post 您的 API 密钥。 ;)