3

The below script block on ColdFUsion 11 has GetHttpRequestData().content as hello

If I change the verb to DELETE it is empty.

So ...

  • Does ColdFusion not support this when making requests via cfhttp?
  • Is this the wrong way?
  • Is there a workaround?

Code:

cfhttp(method="POST", charset="utf-8", url="http://x/showrequest.cfm",   result="result" ) {
    cfhttpparam(name="body", type="body", value="hello");
}

writeOutput(result.filecontent);abort;
James A Mohler
  • 11,060
  • 15
  • 46
  • 72
AdiFatLady
  • 368
  • 1
  • 6
  • 18
  • 1
    The body content is not required for DELETE requests - http://stackoverflow.com/a/299696/1636917 – Miguel-F Mar 16 '16 at 18:48
  • @Miguel-F - True, though I suspect some API's do use it. Sounds like the OP may be working with such an API and that cfhttp may not return the value (which would not surprise me). – Leigh Mar 16 '16 at 20:09
  • You may be correct @Leigh. It is hard to tell with the lack of detail in the question. – Miguel-F Mar 16 '16 at 22:27
  • The API is cloudflares and it requires a message body for the delete operation, https://api.cloudflare.com/#zone-purge-individual-files-by-url-and-cache-tags – AdiFatLady Mar 17 '16 at 11:01
  • Did you search the bug database? Just guessing, but I suspect cfhttp does not send a "body" with DELETE requests. Since it is not required, that feature may not be implemented. If that is the case, you may need to use another tool, ie curl, urlconnection, etc... – Leigh Mar 17 '16 at 19:35

1 Answers1

1

Work around was to use java (shiver). Im sure there are helper libs to do this more succinctly but here it is.

<cfscript>
var u = createObject("java", "java.net.URL").init("https://api.cloudflare.com/client/v4/zones/#site.zoneId#/purge_cache");
var req = u.openConnection();
req.setRequestMethod("DELETE");
req.setDoOutput(true);
req.setRequestProperty("Content-Type", "application/json" );
req.setRequestProperty("X-Auth-Email", "xxxxx" );
req.setRequestProperty("X-Auth-Key", "xxxx" );
var os = req.getOutputStream();

os.write(javaCast("string",'{"files":#serializeJSON(urls)#}').getBytes("UTF-8"));
os.close();
ret = req.getResponseMessage();

var i = req.getInputStream();
var br = createObject("java",   "java.io.BufferedReader").init(createObject("java", "java.io.InputStreamReader").init(i));
var sb = createObject("java", "java.lang.StringBuilder").init();

var line = br.readLine();
while(!isNull(line)){
    sb.append(line);
    line = br.readLine();
}
req.disconnect();


</cfscript>
<cfdump var="req.getResponseCode() = #req.getResponseCode()#">
<cfdump var="#ret#">
<cfdump var="#sb.toString()#">
AdiFatLady
  • 368
  • 1
  • 6
  • 18
  • Do not forget to check the status code and error stream, if needed, so you know if an unexpected error occurred. http://stackoverflow.com/questions/18153249/how-do-i-see-the-message-for-a-http-response-code-406-exception/18154731#18154731 – Leigh Mar 18 '16 at 16:30