CFHTTP 和 CloudFlare API:删除 purge_everything 不工作

CFHTTP & CloudFlare API: DELETE purge_everything not working

还有其他人能够使用 CFHTTP 删除 purge_everything 吗? 我似乎无法让 ColdFusion CFHTTP 成功清除 CloudFlare 区域的缓存。但是我可以做其他事情,比如列表区域等。所以我知道我可以成功地将 CFHTTP 连接到 CloudFlare 的 API。

这是 curl 命令,有效:

curl -svX DELETE -H 'X-Auth-Email: a@b.c' -H 'X-Auth-Key: XYZ' https://api.cloudflare.com/client/v4/zones/xxxxxxx/purge_cache  -H 'Content-Type: application/json' --data '{"purge_everything":true}'

返回的错误是:

{"success":false,"errors":[{"code":1012,"message":"Request must contain one of \"purge_everything\" or \"files\", or \"tags"}],"messages":[],"result":null}

我尝试了很多代码组合...这些是我尝试过的不同变量:

<cfset stFields = '{"purge_everything":true}'>
<cfset stFieldsJson = {"purge_everything":true}>
<cfset stFieldsJson2 = {
    "fields" : {
        "purge_everything"  : true
    }
}>  
<cfset stFieldsJson3 = {
    "purge_everything"  : true,
    "fields" : {
        "purge_everything"  : true
    }
}>  

<cfset tmp = {} />
<cfset tmp['purge_everything'] = true />

...这里是我进行的一些不同调用组合...

<cfhttp url="https://api.cloudflare.com/client/v4/zones/4da78b2707f9753eb79a93d505b4d0d3/purge_cache" method="DELETE" result="cFlare"  charset="utf-8">
    <cfhttpparam type="header" name="X-Auth-Email" value="a@b.c">
    <cfhttpparam type="header" name="X-Auth-Key" value="XYZ">
    <cfhttpparam type="header" name="Content-Type"  value="application/json; charset=utf-8">
    <cfhttpparam type="header" name="accept"  value="*/*">
    <cfhttpparam type="body" value="#serializeJson(stFieldsJson)#" encoded="false">
</cfhttp>   
<cfdump var="#cFlare#"><Cfflush>

<cfhttp url="https://api.cloudflare.com/client/v4/zones/4da78b2707f9753eb79a93d505b4d0d3/purge_cache" method="DELETE" result="cFlare"  charset="utf-8">
    <cfhttpparam type="header" name="X-Auth-Email" value="a@b.c">
    <cfhttpparam type="header" name="X-Auth-Key" value="XYZ">
    <cfhttpparam type="header" name="Content-Type"  value="application/json; charset=utf-8">
    <cfhttpparam type="header" name="accept"  value="*/*">
    <cfhttpparam type="body" value="#serializeJson(stFieldsJson2)#" encoded="false">
</cfhttp>   
<cfdump var="#cFlare#"><Cfflush>

<cfhttp url="https://api.cloudflare.com/client/v4/zones/4da78b2707f9753eb79a93d505b4d0d3/purge_cache" method="DELETE" result="cFlare">
    <cfhttpparam type="header" name="X-Auth-Email" value="a@b.c">
    <cfhttpparam type="header" name="X-Auth-Key" value="XYZ">
    <cfhttpparam type="header" name="Content-Type"  value="application/json">
    <cfhttpparam type="header" name="accept"  value="*/*">
    <cfhttpparam type="body" value="#serializeJson(stFieldsJson3)#" encoded="false">
</cfhttp>   
<cfdump var="#cFlare#"><Cfflush>

<cfhttp url="https://api.cloudflare.com/client/v4/zones/4da78b2707f9753eb79a93d505b4d0d3/purge_cache" method="DELETE" result="cFlare">
    <cfhttpparam type="header" name="X-Auth-Email" value="a@b.c">
    <cfhttpparam type="header" name="X-Auth-Key" value="XYZ">
    <cfhttpparam type="header" name="Content-Type"  value="application/json">
    <cfhttpparam type="header" name="accept"  value="*/*">
    <cfhttpparam type="body" value="#serializeJson(tmp)#" encoded="false">
</cfhttp>   
<cfdump var="#cFlare#"><Cfflush>

<cfhttp url="https://api.cloudflare.com/client/v4/zones/4da78b2707f9753eb79a93d505b4d0d3/purge_cache" method="DELETE" result="cFlare"  charset="utf-8">
    <cfhttpparam type="header" name="X-Auth-Email" value="a@b.c">
    <cfhttpparam type="header" name="X-Auth-Key" value="XYZ">
    <cfhttpparam type="header" name="Content-Type"  value="application/json; charset=utf-8">
    <cfhttpparam type="header" name="accept"  value="*/*">
    <cfhttpparam type="body" value='{"purge_everything":true}' encoded="false">
</cfhttp>   
<cfdump var="#cFlare#"><Cfflush>

<cfhttp url="https://api.cloudflare.com/client/v4/zones/4da78b2707f9753eb79a93d505b4d0d3/purge_cache" method="DELETE" result="cFlare"  charset="utf-8">
    <cfhttpparam type="header" name="X-Auth-Email" value="a@b.c">
    <cfhttpparam type="header" name="X-Auth-Key" value="XYZ">
    <cfhttpparam type="header" name="Content-Type"  value="application/json; charset=utf-8">
    <cfhttpparam type="header" name="accept"  value="*/*">
    <cfhttpparam type="body" value='"purge_everything":true' encoded="false">
</cfhttp>   
<cfdump var="#cFlare#"><Cfflush>

<cfhttp url="https://api.cloudflare.com/client/v4/zones/4da78b2707f9753eb79a93d505b4d0d3/purge_cache" method="DELETE" result="cFlare"  charset="utf-8">
    <cfhttpparam type="header" name="X-Auth-Email" value="a@b.c">
    <cfhttpparam type="header" name="X-Auth-Key" value="XYZ">
    <cfhttpparam type="header" name="Content-Type"  value="application/json; charset=utf-8">
    <cfhttpparam type="header" name="accept"  value="*/*">
    <cfhttpparam type="body" value='purge_everything' encoded="false">
</cfhttp>   
<cfdump var="#cFlare#"><Cfflush>

我也试过使用和不使用 'Encoded' Body 属性,在所有地方使用和不使用 'Charset' 属性。

感谢任何帮助。

不确定您使用的是哪个版本的 CF 运行。但是,我怀疑您没有做错任何事,但是 <cfhttp> 根本没有在 method="DELETE" 时发送 body,考虑到错误消息,这是有道理的。

一种简单的测试方法是将您的 <cfhttp> 调用指向本地 CF 服务器上的测试页面。在测试页面上转储 GetHttpRequestData() 以便您可以查看实际 headers 和提交的内容。 (另一种选择是在开放端口上使用 built in TCPMonitor,它提供了有关请求和响应的更多详细信息。但是,对于这种情况,第一种方法最简单。)

测试页

<!--- echo request headers and content --->
<cfdump var="#getHTTPRequestData()#">

请求

<!--- simulate request --->
<cfset requestBody["purge_everything"] =  true>
<cfhttp url="http://localhost/testPage.cfm" method="DELETE" result="cFlare"  charset="utf-8" >
    <cfhttpparam type="header" name="X-Auth-Email" value="a@b.c">
    <cfhttpparam type="header" name="X-Auth-Key" value="XYZ">
    <cfhttpparam type="header" name="Content-Type"  value="application/json; charset=utf-8">
    <cfhttpparam type="header" name="accept"  value="*/*">
    <cfhttpparam type="body" value="#serializeJson(requestBody)#" encoded="false">
</cfhttp>  

<!--- display request headers and content --->
<cfoutput>#cFlare.fileContent#</cfoutput>

注意到 method="DELETE" 时内容或 body 为空?但是,将其更改为 method="POST" 内容神奇地出现了。

Sending a body with a DELETE request should be valid, so it sounds like a bug. If so, you will need to find a different tool to make the http request, such as invoking curl.exe from cfexecute, or using a custom tag like cfx_http5, or use java classes like as URLConnection or Apache's HTTPClient.

阅读一些文档后,CloudFlare API 似乎正在以 CFHTTP 不会的方式混合方法。

CloudFlare 的 API 声明使用 "application/json" 的 DELETE 方法和内容类型 header。他们的确切例子是:

$ curl -X DELETE "https://api.cloudflare.com/client/v4/zones/023e105f4ecef8ad9ca31a8372d0c353/purge_cache" \
-H "X-Auth-Email: user@example.com" \
-H "X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41" \
-H "Content-Type: application/json" \
--data '{"purge_everything":true}'

然而,当使用 --data 时,curl 将 POST 和 content-type application/x-www-form-urlencoded。 https://curl.haxx.se/docs/manpage.html#-d

然而,CFHTTP 遵循规范。 POST用于发送数据。 DELETE 用于删除 URI。

当我使用 POST 的 CFHTTP 时,我收到一个错误,指出 URI 只接受 DELETE。当我使用 DELETE 删除 CFHTTP 时,没有内容被 POSTed.

我的解决方法是使用他们的 v1 API,而不超出标准 CFML。它像冠军一样工作。 https://www.cloudflare.com/docs/client-api.html