王志軍
王志軍

Reputation: 1021

How to perform DELETE in Elixir using HTTPoison?

I want to execute the command below in Elixir using HTTPoison Library.

$ curl -X DELETE -H "expired: 1442395800" -H "signature: ******************" -d '{"api_key":"***************","delete_path":"*******","expired":"****"}' https://cdn.idcfcloud.com/api/v0/caches
{"status":"success","messages":"We accept the cache deleted successfully."}

When I check the document how to DELETE in HTTPoison

def delete!(url, headers \\ [], options \\ []),   do: request!(:delete, url, "", headers, options)

There are only url and header needed. So where should I put the request body(the json body in curl)?

In Elixir, I tried

req_body = "{\"api_key\":\"#{api_key}\",\"delete_path\":\"#{delete_path}\",\"expired\":\"#{expired}\"}"

url = "https://cdn.idcfcloud.com/api/v0/caches"                                                                                           

response = HTTPoison.delete!(url, header, [req_body])

But is seems not work. Can someone tell how to do it the right way please?

Upvotes: 9

Views: 1248

Answers (1)

Gazler
Gazler

Reputation: 84140

As you have identified, HTTPoison.delete!/3 will send a "" as the post body. There have been questions here before about if a body is valid for a DELETE request - see Is an entity body allowed for an HTTP DELETE request?

You can however bypass this function and call request!/5 directly:

req_body = "{\"api_key\":\"#{api_key}\",\"delete_path\":\"#{delete_path}\",\"expired\":\"#{expired}\"}"

url = "https://cdn.idcfcloud.com/api/v0/caches"                                                                                           

response = HTTPoison.request!(:delete, url, req_body, header)

I have answered a different question which gives more information about generating a post body - Create Github Token using Elixir HTTPoison Library

Upvotes: 8

Related Questions