Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to make HTTP Delete request in my ruby code using Net::HTTP

Tags:

rest

http

ruby

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

like image 500
Kris Avatar asked Oct 10 '12 14:10

Kris


3 Answers

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

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

like image 171
hrnt Avatar answered Oct 12 '22 07:10

hrnt


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}"
like image 35
neoneye Avatar answered Oct 12 '22 08:10

neoneye


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
like image 22
Dorian Avatar answered Oct 12 '22 07:10

Dorian