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

王志軍*_*王志軍 9 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."}
Run Code Online (Sandbox Code Playgroud)

当我检查的文件如何DELETEHTTPoison

def delete!(url, headers \\ [], options \\ []),   do: request!(:delete, url, "", headers, options)
Run Code Online (Sandbox Code Playgroud)

只有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])
Run Code Online (Sandbox Code Playgroud)

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

Gaz*_*ler 7

如您所知,HTTPoison.delete!/3将发送一个""作为帖子正文.之前有一些问题,如果一个主体对DELETE请求有效 - 请参阅实体主体是否允许HTTP DELETE请求?

但是,您可以绕过此功能并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)
Run Code Online (Sandbox Code Playgroud)

我已回答了一个不同的问题,它提供了有关生成帖子正文的更多信息 - 使用Elixir HTTPoison Library创建Github令牌