如何使用HTTParty实现此POST请求?

Joh*_*son 14 ruby curl httparty

我很难使用Ruby的HTTParty库向API端点发出POST请求.我正在与之交互的API是Gittip API,它们的端点需要身份验证.我已经能够使用HTTParty成功地进行经过身份验证的GET请求.

您可以在示例代码中看到:

user = "gratitude_test"
api_key = "5962b93a-5bf7-4cb6-ae6f-aa4114c5e4f2"
# I have included real credentials since the above is merely a test account.

HTTParty.get("https://www.gittip.com/#{user}/tips.json", 
             { :basic_auth => { :username => api_key } })
Run Code Online (Sandbox Code Playgroud)

该请求有效并按预期返回以下内容:

[
  {
    "amount" => "1.00",
    "platform" => "gittip",
    "username" => "whit537"
  },
  {
    "amount" => "0.25",
    "platform" => "gittip",
    "username" => "JohnKellyFerguson"
  }
]
Run Code Online (Sandbox Code Playgroud)

但是,我无法使用HTTParty成功发出POST请求.Gittip API描述了使用curl发出POST请求,如下所示:

curl https://www.gittip.com/foobar/tips.json \
  -u API_KEY: \
  -X POST \
  -d'[{"username":"bazbuz", "platform":"gittip", "amount": "1.00"}]' \
  -H"Content-Type: application/json"
Run Code Online (Sandbox Code Playgroud)

我尝试使用HTTParty(不成功)构建我的代码,如下所示:

user = "gratitude_test"
api_key = "5962b93a-5bf7-4cb6-ae6f-aa4114c5e4f2"

HTTParty.post("https://www.gittip.com/#{user}/tips.json",
              { 
                :body => [ { "amount" => "0.25", "platform" => "gittip", "username" => "whit537" } ],
                :basic_auth => { :username => api_key },
                :headers => { 'Content-Type' => 'application/json' }
               })
Run Code Online (Sandbox Code Playgroud)

第一个参数是url,第二个参数是选项哈希.当我运行上面的代码时,我收到以下错误:

NoMethodError: undefined method `bytesize' for [{"amount"=>"0.25", "platform"=>"gittip", "username"=>"whit537"}]:Array
  from /Users/John/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http/generic_request.rb:179:in `send_request_with_body'
Run Code Online (Sandbox Code Playgroud)

我已经尝试了构造API调用的各种其他组合,但无法弄清楚如何使其工作.这是另一个这样的例子,我没有使用数组作为正文的一部分并转换内容to_json.

user = "gratitude_test"
api_key = "5962b93a-5bf7-4cb6-ae6f-aa4114c5e4f2"

HTTParty.post("https://www.gittip.com/#{user}/tips.json",
          {
            :body => { "amount" => "0.25", "platform" => "gittip", "username" => "whit537" }.to_json,
            :basic_auth => { :username => api_key },
            :headers => { 'Content-Type' => 'application/json' }
           })
Run Code Online (Sandbox Code Playgroud)

返回以下内容(500错误):

<html>
  <head>
    <title>500 Internal Server Error</title>
  </head>
  <body>\n        Internal server error, program!\n        <pre></pre>  
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

我对curl并不熟悉,所以我不确定我是否错误地将内容翻译成了HTTParty.

任何帮助,将不胜感激.谢谢.

d_e*_*ier 30

只是一个猜测,但看起来你正在预期JSON时传递一个哈希值.

尝试将:body声明替换为:

:body => [{ "amount" => "0.25", 
             "platform" => "gittip", 
             "username" => "whit537" }].to_json
Run Code Online (Sandbox Code Playgroud)

编辑:我建议使用to_json序列化程序,但是将它放在哈希而不是数组之后放错了它并完全删除了数组.该示例使用多个记录,因此该数组是必需的.

看完这个帖子后,看起来Gittip对于接受标题很挑剔.

:headers => { 'Content-Type' => 'application/json', 'Accept' => 'application/json'}
Run Code Online (Sandbox Code Playgroud)

所以,完整的建议是:

HTTParty.post("https://www.gittip.com/#{user}/tips.json",
  { 
    :body => [ { "amount" => "0.25", "platform" => "gittip", "username" => "whit537" } ].to_json,
    :basic_auth => { :username => api_key },
    :headers => { 'Content-Type' => 'application/json', 'Accept' => 'application/json'}
  })
Run Code Online (Sandbox Code Playgroud)