views:

147

answers:

3

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.

A: 

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
is there a pure CFML solution you think?
Henry
Here's how's done in cfrest google group: http://cfrest.googlegroups.com/web/apiUtilities.cfc?gda=GD5yz0IAAABIZNuY_A2GQeIp6iDWbK7VaStTUa30d_821IbfLDQgDohJfanwNHy4DV68OGE7zINV4u3aa4iAIyYQIqbG9naPgh6o8ccLBvP6Chud5KMzIQ
zarko.susnjar
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
also this http://restfulcf.riaforge.org/
zarko.susnjar
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
@zarko - the cfrest.googlegroups link helped me out. It appears as though I was just passing in the wrong headers. Thanks!
Jason
cfhttp supports the put verb in HTTP. http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7ffc.htmlI won't dispute that the java method works, but cfhttp isn't limited to just get and post.
Terry Ryan
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
A: 

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

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