Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 836
王志軍 Avatar asked Sep 16 '15 09:09

王志軍


1 Answers

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

like image 85
Gazler Avatar answered Sep 23 '22 07:09

Gazler