5

What would this code look like in ColdFusion?

  protected function httpPut($url, $params = null, $data = null)
  {
      $fh = fopen('php://memory', 'rw');
          fwrite($fh, $data);
          rewind($fh);

    $ch = curl_init($url);
    $this->addOAuthHeaders($ch, $url, $params['oauth']);
    curl_setopt($ch, CURLOPT_PUT, 1);
    curl_setopt($ch, CURLOPT_INFILE, $fh);
    curl_setopt($ch, CURLOPT_INFILESIZE, strlen($data));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $resp  = $this->curl->addCurl($ch);
    fclose($fh);
    return $resp;
  }

I have something like the following, but it doesn't seem to be working.

<cffile action="write" file="d:\my\directory\path\test.xml" output="#arguments.requestXML#">
<cfhttp url="#oaAccessTokenURL#" method="#arguments.requestType#" charset="UTF-8">
    <cfheader name="Authorization" value="#oauthheader#">
    <cfhttpparam type="file" name="Course" file="d:\my\directory\path\test.xml">    
</cfhttp>

I don't know enough about PHP to understand how the $data variable (which is just a string of XML data) is getting put into the http request and how to duplicate that in ColdFusion.

Jason
  • 17,276
  • 23
  • 73
  • 114
  • 1
    I am not php guru either. But my guess would be it is sending the XML in the "body" of the request. Try using cfhttpparam type="body" and use the #requestXML# as the value. – Leigh Aug 09 '10 at 20:05
  • In Coldfusion installation there's already Apache http library, check how it could be done in Java, probably fastest and most customizable solution I could think of right now. – zarko.susnjar Aug 09 '10 at 20:08
  • @Leigh - I just tried it and am still getting an error. If it helps any, the error I'm getting is: Unsupported media type 'application/octet-stream' – Jason Aug 09 '10 at 20:10
  • http://stackoverflow.com/questions/1051004/how-to-send-put-delete-http-request-in-httpurlconnection-looks-like-not-workin – zarko.susnjar Aug 09 '10 at 20:11
  • 1
    @Jason - What is the expected content-type on the receiving end (text/xml)? It sounds like you may need to add a content-type header to the cfhttp call. – Leigh Aug 09 '10 at 20:16
  • @Leigh - when I add the content type header the page really blows up. It doesn't seem to error on the http put request though? It returns the HTML of the resulting page. However, it still is not executing correctly. The API documentation is not very thorough. Here is what I get: – Jason Aug 09 '10 at 20:40
  • Add a new Course to the caller's organization. Request Content: "course" detail XML (name, identifier) Response Content: "course" summary XML ("id" element holds new ID) Errors: duplicate name, client error (content invalid) – Jason Aug 09 '10 at 20:40
  • @zarko - do you have an example of how I might do that. I don't have much experience writing java with ColdFusion. – Jason Aug 09 '10 at 20:41
  • 1
    @Jason - You are probably just missing some parameters or headers. If you really cannot get it to work with CF, then try java as @zarko suggested. If that works, I would view the headers of a succesful request with something like Live http headers. Then try and duplicate it with cfhttp. (I love java, but try and use it only when I cannot make something work with vanilla CF) – Leigh Aug 09 '10 at 21:06

3 Answers3

1

I would try adding method="put" to your cfhttp call. That will make CFHTTP send the correct http verb (PUT in this case).

Terry Ryan
  • 1,911
  • 15
  • 13
0

Here's Java spark (from Java docs), you need to work it out:

PutMethod put = new PutMethod("http://jakarta.apache.org");
        put.setRequestBody(new FileInputStream("UploadMe.gif"));

is translated in CF like this:

<cfset myPut  = createObject("java", "org.apache.commons.httpclient.methods.PutMethod") />
<cfset myPut.init("http://example.com") />
<cfset myInputStream = createObject("java", "java.io.FileInputStream") />
<cfset myInputStream.init("myxml.xml") />
<cfset myPut.setRequestBody(myInputStream) />

And so on...

In link I pasted above you can see somehting like this:

    URL url = new URL("http://www.example.com/resource");
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestMethod("PUT");
OutputStreamWriter out = new OutputStreamWriter(
    httpCon.getOutputStream());
out.write("Resource content");
out.close();

Find working Java soution and translate it in CF.

EDIT:

See comments below for a solution.

zarko.susnjar
  • 2,053
  • 2
  • 17
  • 35
  • is there a pure CFML solution you think? – Henry Aug 10 '10 at 04:25
  • 2
    Here's how's done in cfrest google group: http://cfrest.googlegroups.com/web/apiUtilities.cfc?gda=GD5yz0IAAABIZNuY_A2GQeIp6iDWbK7VaStTUa30d_821IbfLDQgDohJfanwNHy4DV68OGE7zINV4u3aa4iAIyYQIqbG9naPgh6o8ccLBvP6Chud5KMzIQ – zarko.susnjar Aug 10 '10 at 06:33
  • And here's how to "attach" file to cfhttp http://www.bennadel.com/blog/619-Throwing-And-Catching-A-File-Using-CFHttp-For-Both-Actions.htm – zarko.susnjar Aug 10 '10 at 06:35
  • So it is possible, if someone created a new question about something, I presume he already tried it and figured out that it's not working for him, so my first idea was to try Java. if cfhttp does the job then super great! :) – zarko.susnjar Aug 10 '10 at 06:42
  • @zarko - the cfrest.googlegroups link helped me out. It appears as though I was just passing in the wrong headers. Thanks! – Jason Aug 10 '10 at 13:42
  • cfhttp supports the put verb in HTTP. http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7ffc.html I won't dispute that the java method works, but cfhttp isn't limited to just get and post. – Terry Ryan Aug 10 '10 at 22:44
  • Yes, Terry, at first I thought it is not, but then I wrote a comment which pointed Jason to a solution. He accepted the answer so I didn't remove my Java code. – zarko.susnjar Aug 11 '10 at 04:39
0

Assuming you are doing a PUT method, you can use ColdFusion's GetHttpRequestData() function to obtain the XHR data.

You can then save it out by doing something like this:

<cfset xhr_data = GetHttpRequestData() />
<cffile action="write" file="PATH/FILENAME" output="#xhr_data.content#">
stldoug
  • 853
  • 9
  • 14