如何使用HTTPoison在Elixir中执行DELETE?

时间:2015-09-16 09:49:45

标签: curl elixir http-delete

我想使用HTTPoison Library在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)

只需要urlheader。那么我应该在哪里放置请求主体(json主体在curl)?

在Elixir,我试过

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])

但似乎行不通。有人能说出正确的方法吗?

1 个答案:

答案 0 :(得分:7)

如您所知,HTTPoison.delete!/3将发送""作为帖子正文。如果一个正文对DELETE请求有效,那么之前就存在一些问题 - 请参阅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)

我已回答了另一个问题,该问题提供了有关生成帖子正文的更多信息 - Create Github Token using Elixir HTTPoison Library