How to execute DELETE in Elixir using HTTPoison?

I want to execute the command below in Elixir using the 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 validate a document as DELETE in HTTPoison

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

Only url and header required. So where should I put the request body (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 it doesn't seem to work. Can anyone tell how to do this correctly?

+8
curl elixir
source share
1 answer

As you have already defined, HTTPoison.delete!/3 will send "" as the body of the message. There have been questions before about whether the body is valid for a DELETE request - see Is the body of an object allowed for an HTTP DELETE request?

However, you can bypass this function and directly call request!/5 :

 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 answered another question that gives more information about creating a message body - Create a Github token using the HTTP -ison Elixir library

+6
source

All Articles