Reading A Remote File With CFHTTP Using Railo And ColdFusion

Author: Steven Neiland
Published:

Warning: This blog entry was written two or more years ago. Therefore, it may contain broken links, out-dated or misleading content, or information that is just plain wrong. Please read on with caution.

One of the new features I rolled last week on my site is an embedded QR code in the print layout. To do this I use cfhttp to pull the corresponding qr code image file of some shortened url from the bitly service. I then either store it in the database for later retrieval or output it directly to the browser.

However when I deployed this feature to my server I encountered a small incompatibility between the way Railo and Adobe ColdFusion use cfhttp to return a bytearray.

Using CFHTTP To Read A Remote File Binary With ColdFusion

Here is the code I used to accomplish this on my local machine which runs Adobe ColdFusion.

Note how on the second line I call toByteArray() on the filecontent value. This is necessary because as I understand it in ACF "fileContent" contains a ByteArrayOutputStream Object.

<!--- Call the bit.ly service --->
<cfhttp url="http://bit.ly/{shortUrlString}.qrcode" result="xmlResponse"/>

<!--- Extract the filecontent and cast to a byte array --->
<cfset qrcodeBinary = xmlResponse.fileContent.toByteArray() />

Using CFHTTP To Read A Remote File Binary With Railo

When I deployed this code from my developer machine running Adobe ColdFusion to my server which runs Railo I got this error on trying to call the toByteArray() method.

No matching Method/Function for fileContent.toByteArray() found

After a little searching around I discovered that in Railo there is no toByteArray() method because it simply is not needed. The filecontent variable is already a true byte array so all I had to do was drop the toByteArray() call.

<!--- Call the bit.ly service --->
<cfhttp url="http://bit.ly/{shortUrlString}.qrcode" result="xmlResponse"/>

<!--- Extract the filecontent byte array --->
<cfset qrcodeBinary = xmlResponse.fileContent />

Its a small but significant difference.

Reader Comments

  • Please keep comments on-topic.
  • Please do not post unrelated questions or large chunks of code.
  • Please do not engage in flaming/abusive behaviour.
  • Comments that contain advertisments or appear to be created for the purpose of link building, will not be published.

Archives Blog Listing