我想使用HTTPoison库在Elixir中执行下面的命令。
$ 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."}当我检查文档如何在DELETE中使用HTTPoison时
def delete!(url, headers \\ [], options \\ []), do: request!(:delete, url, "", headers, options)只需要url和header。那么,我应该把请求体(curl中的json主体)放在哪里呢?
在长生不老药里,我试过
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])但似乎不起作用。有人能告诉我怎么用正确的方法吗?
发布于 2015-09-16 10:00:01
正如您已经确定的,HTTPoison.delete!/3将发送一个""作为post主体。在此之前,有一些关于删除请求的主体是否有效的问题-请参阅Is an entity body allowed for an HTTP DELETE request?。
但是,您可以绕过这个函数直接调用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)我回答了一个不同的问题,它提供了更多关于生成post体的信息-- Create Github Token using Elixir HTTPoison Library。
https://stackoverflow.com/questions/32605174
复制相似问题