Unable to make HTTP Delete request in my ruby code

2019-03-30 14:45发布

Im using Net::HTTP in my ruby code to make http requests. For example to make a post request i do

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

This works. But im unable to make a delete request, i.e.

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

gives the following error

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

The documentation at http://ruby-doc.org/stdlib-1.9.3/libdoc/net/http/rdoc/Net/HTTP.html shows Delete is available. So why is it not working in my case ?

Thank You

标签: ruby http rest
3条回答
时光不老,我们不散
2楼-- · 2019-03-30 14:47
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}"
查看更多
我只想做你的唯一
3楼-- · 2019-03-30 14:57

The documentation tells you that Net::HTTP::Delete is a class, not a method.

Try Net::HTTP.new('www.server.com').delete('/path') instead.

查看更多
太酷不给撩
4楼-- · 2019-03-30 15:01

Simple post and delete requests, see docs for more:

puts Net::HTTP.new("httpbin.org").post("/post", "a=1").body
puts Net::HTTP.new("httpbin.org").delete("/delete").body
查看更多
登录 后发表回答