【问题标题】:CFHTTP & CloudFlare API: DELETE purge_everything not workingCFHTTP & CloudFlare API:删除 purge_everything 不起作用
【发布时间】:2016-05-20 05:20:47
【问题描述】:

还有其他人能够使用 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\" 之一,或 \"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' 属性。

感谢任何帮助。

【问题讨论】:

标签: coldfusion cloudflare cfml http-delete cfhttp


【解决方案1】:

不确定您运行的是哪个版本的 CF。但是,我怀疑您没有做错任何事情,但是 &lt;cfhttp&gt; 根本没有在 method="DELETE" 时发送正文,考虑到错误消息,这是有道理的。

一种简单的测试方法是将您的&lt;cfhttp&gt; 调用指向您本地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 应该是有效的,所以这听起来像是一个错误。如果是这样,您将需要找到不同的工具来发出 http 请求,例如从 cfexecute 调用 curl.exe,或使用自定义标记(如 cfx_http5),或使用 java 类如 URLConnectionApache's HTTPClient

【讨论】:

  • 很好的调查,感谢您提出的解决方法。在发布我的问题后,我做了更多的工作并得出了与您相同的结论 - 当方法为 DELETE 时,CFHTTP 不会发布正文。
  • 是的,听起来它没有明确禁止,所以如果有一些迹象表明 cfhttp 打算忽略正文,那就太好了。至少那时人们不会挠头想知道出了什么问题......
【解决方案2】:

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

CloudFlare 的 API 声明使用“application/json”的 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 将使用 content-type application/x-www-form-urlencoded POST。 https://curl.haxx.se/docs/manpage.html#-d

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

当我使用 POST 进行 CFHTTP 时,我收到一个错误,即 URI 只接受 DELETE。当我使用 DELETE CFHTTP 时,没有发布任何内容。

在不脱离标准 CFML 的情况下,我的解决方法是使用他们的 v1 API。它的工作就像一个冠军。 https://www.cloudflare.com/docs/client-api.html

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-06
  • 2021-05-17
  • 2012-10-26
  • 1970-01-01
相关资源
最近更新 更多