如何使用HTTParty实现此POST请求? [英] How can I implement this POST request using HTTParty?

查看:1489
本文介绍了如何使用HTTParty实现此POST请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用Ruby的HTTParty库向API端点发出POST请求。我要与之互动的API是 Gittip API ,其端点需要身份验证。



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

  user =gratitude_test
api_key =5962b93a-5bf7-4cb6-ae6f-aa4114c5e4f2
#我已经包括真正的凭证,因为上述仅仅是一个测试帐户。

HTTParty.get(https://www.gittip.com/#{user}/tips.json,
{:basic_auth => {:username => api_key }})

该请求工作,并按预期返回以下内容:

  [
{
amount=> 1.00,
platform=> gittip,
username=> whit537
},
{
amount=> 0.25,
platform=> gittip,
username=> JohnKelly Ferguson
}
]

使用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}]'\
-HContent-Type:application / json

我已经尝试(不成功)使用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'}
})

第一个参数是url,第二个参数是选项散列。当我运行上面的代码,我得到以下错误:

  NoMethodError:undefined方法`bytesize' =0.25,platform=>gittip,username=>whit537}]:数组
来自/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'


$ b b

我尝试了其他各种组合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'}
})

这会返回以下错误

 < html> 
< head>
< title> 500内部伺服器错误< / title>
< / head>
< body> \\\
内部服务器错误,程序!\ n< pre>< / pre>
< / body>
< / html>

我不太熟悉curl,所以我不确定我是否翻译错误到HTTParty。



任何帮助将不胜感激。谢谢。

解决方案

只是一个猜测,但看起来你正在JSON中传递一个哈希。 / p>

尝试将:body 替换为:

 :body => [{amount=> 0.25,
platform=> gittip,
username=> whit537}]。to_json

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



查看 this thread ,看起来像Gittip很挑剔的接受头。

 :headers => {'Content-Type'=> 'application / json','Accept'=> 'application / json'} 

因此,完整的建议是:

  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'}
} )


I am having difficulty making a POST request to an API endpoint using Ruby's HTTParty library. The API I'm interacting with is the Gittip API and their endpoint requires authentication. I have been able to successfully make an authenticated GET request using HTTParty.

You can see in the example code:

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 } })

That request works and returns the following as expected:

[
  {
    "amount" => "1.00",
    "platform" => "gittip",
    "username" => "whit537"
  },
  {
    "amount" => "0.25",
    "platform" => "gittip",
    "username" => "JohnKellyFerguson"
  }
]

However, I have been unable to make a successful POST request using HTTParty. The Gittip API describes making a POST request using curl as follows:

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"

I have tried (unsuccessfully) structuring my code using HTTParty as follows:

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' }
               })

The first argument is the url and the second argument is an options hash. When I run the code above, I get the following error:

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'

I have tried various other combinations of structuring the API call, but can't figure out how to get it to work. Here's another such example, where I do not user an array as part of the body and convert the contents 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' }
           })

Which returns the following (a 500 error):

<html>
  <head>
    <title>500 Internal Server Error</title>
  </head>
  <body>\n        Internal server error, program!\n        <pre></pre>  
  </body>
</html>

I'm not really familiar with curl, so I'm not sure if I am incorrectly translating things to HTTParty.

Any help would be appreciated. Thanks.

解决方案

Just a guess, but it looks like you're passing a hash in the body when JSON is expected.

Try replacing the :body declaration with:

:body => [{ "amount" => "0.25", 
             "platform" => "gittip", 
             "username" => "whit537" }].to_json

Edit: I suggested the to_json serializer, but misplaced it by putting it after the hash instead of the array and removing the array altogether. The example uses multiple records, so the array is necessary.

After looking at this thread, it looks like Gittip is picky about the accept header.

:headers => { 'Content-Type' => 'application/json', 'Accept' => 'application/json'}

So, the full suggestion is:

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'}
  })

这篇关于如何使用HTTParty实现此POST请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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