Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 如何使用HTTParty实现此POST请求?_Ruby_Curl_Httparty - Fatal编程技术网

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

Ruby 如何使用HTTParty实现此POST请求?,ruby,curl,httparty,Ruby,Curl,Httparty,我很难使用Ruby的HTTParty库向API端点发出POST请求。与我交互的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

我很难使用Ruby的HTTParty库向API端点发出POST请求。与我交互的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 } })
该请求正常工作并按预期返回以下内容:

[
  {
    "amount" => "1.00",
    "platform" => "gittip",
    "username" => "whit537"
  },
  {
    "amount" => "0.25",
    "platform" => "gittip",
    "username" => "JohnKellyFerguson"
  }
]
但是,我无法使用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"
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' }
               })
我已尝试(未成功)使用HttpParty构建我的代码,如下所示:

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"
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 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'
我尝试过构造API调用的各种其他组合,但不知道如何让它工作。这里是另一个这样的例子,我不使用数组作为主体的一部分,并将内容
转换为_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' }
           })
返回以下内容(500错误):


500内部服务器错误
\n内部服务器错误,程序错误\N
:body => [{ "amount" => "0.25", 
             "platform" => "gittip", 
             "username" => "whit537" }].to_json
我对curl不是很熟悉,所以我不确定是否将内容错误地翻译到了HTTParty


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

只是一个猜测,但是当需要JSON时,看起来您在正文中传递了一个哈希

尝试将
:body
声明替换为:

:headers => { 'Content-Type' => 'application/json', 'Accept' => 'application/json'}
编辑: 我向_json序列化程序推荐了
,但由于将它放在散列之后而不是数组之后,并完全删除了数组,因此将其放错了位置。该示例使用多条记录,因此需要使用数组

查看之后,Gittip似乎对accept头很挑剔

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'}
  })
因此,完整的建议是:


嘿,谢谢,我刚刚尝试了你的建议,但仍然得到了与以前相同的错误。更新了答案,你可能也想删除数组声明
bytesize
是一个字符串方法,因此在哈希或数组上执行它将不起作用。您可以看到ruby源代码。我尝试了您的建议,但没有使用数组,结果出现了500个错误。我编辑了我的帖子,详细描述了发生的事情。哇,这很有效!!!非常感谢你!!!我已经为此挣扎了一段时间,所以我非常感谢你的帮助。@cheshireoctopus,我不是100%确定。我基于这个例子的答案似乎表明标题是必要的。