有没有其他人能够使用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”:“请求必须 包含\“purge_everything \”或\“files \”之一,或 \ “标签”}], “消息”:[], “结果”:空}
我尝试了很多代码组合......这些是我尝试的不同变量:
<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'Fody属性,在所有地方都使用和不使用'Charset'属性。
感谢任何帮助。
答案 0 :(得分:2)
不确定您正在运行哪个版本的CF.但是,我怀疑你没有做错任何事,但<cfhttp>
只是在method="DELETE"
时没有发送一个正文,这对于错误信息是有意义的。
测试它的一种简单方法是将<cfhttp>
调用到本地CF服务器上的测试页面。在测试页面转储GetHttpRequestData()
上,以便您可以查看提交的实际标题和内容。 (另一种选择是在开放端口上使用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"
时内容或正文是否为空?但是,将其更改为method="POST"
,内容会神奇地显示出来。
Sending a body with a DELETE request应该有效,所以听起来像个bug。如果是这样,您将需要找到一个不同的工具来发出http请求,例如从cfexecute
调用curl.exe,或使用cfx_http5之类的自定义标记,或者使用像URLConnection之类的java类或Apache's HTTPClient
答案 1 :(得分:0)
在阅读了一些文档之后,似乎CloudFlare API正在以CFHTTP不会的方式混合方法。
CloudFlare的API声明使用&#34; application / json&#34;的DELETE方法和内容类型标题。他们的确切例子是:
$ 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将使用内容类型application / x-www-form-urlencoded进行POST。 https://curl.haxx.se/docs/manpage.html#-d
然而,CFHTTP遵循规范。 POST用于发送数据。 DELETE用于删除URI。当我使用POST进行CFHTTP时,我收到的错误是只接受了DELETE的URI。当我使用DELETE进行CFHTTP时,没有内容被发布。
我的解决方法是在不使用标准CFML的情况下使用他们的v1 API。它像冠军一样工作。 https://www.cloudflare.com/docs/client-api.html