在带有 HTTParty 的 Rails 应用程序中使用 Postmates API [英] Using Postmates API in a rails app with HTTParty

查看:45
本文介绍了在带有 HTTParty 的 Rails 应用程序中使用 Postmates API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直致力于将 Postmates 与我的电子商务 Rails 应用程序集成以实现按需交付.我已经构建了一个用于测试目的的控制器和视图,并在我的路由文件(在我的开发环境中构建)中进行了配置.作为参考,以下是 Postmates 的文档:docs,这里是一篇技术博文:博客

路由文件:

资源:postmates做会员做发布'get_delivery'结尾结尾

控制器:

需要'httparty'需要'json'类 PostmatesController <应用控制器def get_deliveryapi_key = **隐藏**@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 =>{:用户名=>api_key },:headers =>{ '内容类型' =>'应用程序/json' })结尾结尾

视图:

<%= form_tag url_for(:controller => 'postmates', :action => 'get_delivery'), :method =>'post' 做 %><%= submit_tag "Get Delivery", :action =>'get_delivery', :controller =>'邮递员'%><%结束%>

<p>结果:<%=@result%></p>

这是来自 api 的响应:

{"kind"=>"error", "code"=>"invalid_params", "params"=>{"dropoff_address"=>"此字段是必需的.", "pickup_address"=>"此字段为必填项."}, "message"=>"您请求的参数无效."}

在我看来,没有在 HTTP 请求中提交下车和上车地址.谁能告诉我我是否犯了一些小的语法错误或其他什么?我收到此响应的事实意味着我的身份验证很好,url 也是如此.有什么想法吗?

谢谢.

解决方案

看起来您将 JSON 数据发布到 API,而文档 (https://postmates.com/developer/docs#basics) 说您应该将数据发布为 application/x-www-form-urlencoded

我对 HTTParty 不是很熟悉,但您可能只需删除 .to_json 并且不指定内容类型标头.

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.

解决方案

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.

这篇关于在带有 HTTParty 的 Rails 应用程序中使用 Postmates API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆