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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With