5

我在我的 ruby​​ 代码中使用 Net::HTTP 来发出 http 请求。例如发出一个帖子请求我做

require 'net/http'
Net::HTTP.post_form(url,{'email' => email,'password' => password})

这行得通。但我无法发出删除请求,即

require 'net/http'
Net::HTTP::Delete(url)

给出以下错误

NoMethodError: undefined method `Delete' for Net::HTTP:Class

http://ruby-doc.org/stdlib-1.9.3/libdoc/net/http/rdoc/Net/HTTP.html上的文档显示删除可用。那么为什么它在我的情况下不起作用?

谢谢你

4

3 回答 3

6

文档告诉您 Net::HTTP::Delete 是一个类,而不是一个方法。

试试Net::HTTP.new('www.server.com').delete('/path')吧。

于 2012-10-10T14:23:33.337 回答
5
uri = URI('http://localhost:8080/customer/johndoe')
http = Net::HTTP.new(uri.host, uri.port)
req = Net::HTTP::Delete.new(uri.path)
res = http.request(req)
puts "deleted #{res}"
于 2014-05-12T18:55:48.883 回答
1

简单的发布和删除请求,请参阅文档了解更多信息:

puts Net::HTTP.new("httpbin.org").post("/post", "a=1").body
puts Net::HTTP.new("httpbin.org").delete("/delete").body
于 2017-04-26T23:50:47.187 回答