3

I am communicating with API that requires DELETE request with JSON body. This works on console:

curl -XDELETE http://api.com/endpoint_path/rest_resource -d '{"items":[{"type":"type1","item_id":"item1"}]}'

It seems that most gems for making HTTP requests don't support DELETE request with body (I tried RestClient and Curb). Is there a way to do it using some Ruby gem (preferably Curb) or Net::HTTP?

Lukas Stejskal
  • 2,542
  • 19
  • 30

3 Answers3

6

Here's one way using HTTParty:

HTTParty.delete("http://api.com/endpoint_path/rest_resource", { 
  :body => '{"items":[{"type":"type1","item_id":"item1"}]}'
})
Casper
  • 33,403
  • 4
  • 84
  • 79
0

it could be used her. This is ORM for api. https://github.com/remiprev/her

Example of usage:

RestResource.destroy_existing(id, body_params)
gayavat
  • 18,910
  • 11
  • 45
  • 55
0

I also spent some time on this issue and @Casper's answer shed the light.

Seems to me that the key is to pass the body value as JSON string, which is not written in most of the documentations I found.

Here's another example using httpclient

require 'json'
body = { 'items': [{ 'type': 'type1', 'item_id': 'item1' }]}
HTTPClient.new.delete('http://api.com/endpoint_path/rest_resource', body.to_json)
Dapeng Li
  • 3,282
  • 1
  • 24
  • 18