make a http delete request in ruby

Here's an example of how to make an HTTP DELETE request using the Ruby net/http library:

main.rb
require 'net/http'

url = URI.parse('http://example.com/resource')
request = Net::HTTP::Delete.new(url.path)

# optional headers
request['User-Agent'] = 'MyApp/1.0'

response = Net::HTTP.start(url.host, url.port) do |http|
  http.request(request)
end

puts response.body
271 chars
14 lines

In this example, you first create a URI object to represent the URL of the resource to delete. Then, you create a new Net::HTTP::Delete object, passing in the path portion of the URL as the argument.

You can set any additional headers you need on the request object using the hash-like []= operator syntax, as shown with the User-Agent header.

Next, you use Net::HTTP.start to establish a connection to the server and send the request. Finally, you can inspect the body of the response.

gistlibby LogSnag