Lukas Stejskal
Lukas Stejskal

Reputation: 2562

How to send DELETE request with body using ruby gem?

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?

Upvotes: 3

Views: 5022

Answers (3)

Dapeng Li
Dapeng Li

Reputation: 3632

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)

Upvotes: 0

Casper
Casper

Reputation: 34338

Here's one way using HTTParty:

HTTParty.delete("http://api.com/endpoint_path/rest_resource", { 
  :body => '{"items":[{"type":"type1","item_id":"item1"}]}'
})

Upvotes: 6

gayavat
gayavat

Reputation: 19418

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

Example of usage:

RestResource.destroy_existing(id, body_params)

Upvotes: 0

Related Questions