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

How to perform DELETE in Elixir using HTTPoison?

我想使用 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 in HTTPoison

def delete!(url, headers \ [], options \ []),   do: request!(:delete, url, "", headers, options)

只需要urlheader。那么我应该把请求正文放在哪里(curl 中的 json 正文)?

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

但是好像不行。谁能告诉我正确的做法吗?

如您所见,HTTPoison.delete!/3 将发送 "" 作为 post 正文。之前这里有关于正文是否对 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)

我回答了一个不同的问题,它提供了有关生成 post 正文的更多信息 -