CFHTTP and CloudFlare APIs: DELETE purge_everything not working

Has anyone else been able to DELETE purge_everything with CFHTTP? I cannot get ColdFusion CFHTTP to successfully clear the CloudFlare zone cache. But I can do other things like list zones, etc. Therefore, I know that I can successfully use CFHTTP for the CloudFlare API.

This is the curl command that works:

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}' 

Error returned:

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

I tried so many code combinations ... these are the different variables I tried:

 <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 /> 

... and here are a few different call combinations that I made ...

 <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> 

I also tried with the "Encoded" Body attribute without and without the "Charset" attribute.

Any help is appreciated.

+5
source share
2 answers

Not sure which version of CF you are using. However, I suspect that you are not doing anything wrong, but <cfhttp> just does not send the body when method="DELETE" , which makes sense given the error message.

An easy way to test this is to point to your <cfhttp> call to the test page on your local CF server. On a dump of the GetHttpRequestData() test page so that you can view the actual headers and content. (Another option is to use the built-in TCPMonitor in the open port, which provides more detailed information about requests and responses. However, for this scenario, the first method is simple.)

Testing Page

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

Request

 <!--- 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> 

Note that the content, or body, is empty when method="DELETE" ? However, change it to method="POST" and the magic will appear in the content.

GetHttpRequestData () - Method = DELETE

Sending a body using a DELETE query must be valid, so this sounds like an error. If so, you will need to find another tool to make an http request, for example, call curl.exe from cfexecute or use a special tag, for example cfx_http5, or use java classes such as URLConnection or Apache HTTPClient .

+2
source

After reading some documents, it looks like the CloudFlare API is mixing methods in such a way that CFHTTP will not.

The CloudFlare API claims to use the DELETE method and the content type header for "application / json". Their exact example:

 $ 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}' 

However, when you use -data, curl will be POST with an application of type content / x -www-form-urlencoded. https://curl.haxx.se/docs/manpage.html#-d

CFHTTP, however, meets specifications. POST is used to send data. DELETE to remove the URI.

When I CFHTTP with POST, I get an error message that is only accepted for DELETE for the URI. When I CFHTTP with DELETE, the content is not sent.

My workaround, without leaving standard CFML, was to use their API v1. He works as a champion. https://www.cloudflare.com/docs/client-api.html

0
source

Source: https://habr.com/ru/post/1242676/


All Articles