首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在带有HTTParty的rails应用程序中使用Postmates

在带有HTTParty的rails应用程序中使用Postmates
EN

Stack Overflow用户
提问于 2015-01-08 22:41:18
回答 2查看 428关注 0票数 2

我一直致力于将Postmates与我的电子商务rails应用程序结合起来,以便按需交付。我已经为测试目的构建了一个控制器和视图,并在我的路由文件(在我的dev环境中构建)中对它进行了配置。作为参考,这里是来自Postmates:文档的文档,这里是一个技术博客:博客

路由文件:

代码语言:javascript
复制
resources :postmates do
    member do 
        post 'get_delivery'
    end
  end

控制员:

代码语言:javascript
复制
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

意见:

代码语言:javascript
复制
<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返回的响应:

代码语言:javascript
复制
{"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也是如此。有什么想法吗?

谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-01-09 01:30:07

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

我不太熟悉HTTParty,但您可能只需删除.to_json,而不指定内容类型的标题。

票数 5
EN

Stack Overflow用户

发布于 2015-01-09 02:08:04

与您的问题无关,但您不应该公开发布API密钥。;)

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27850845

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档