Invoking My First Web Service With ColdFusion (Amazon Web Service)

Posted December 16, 2006 at 11:45 AM

Tags: ColdFusion

Not so long ago, I created my first web service. I figured it was time to see how the other half live (those that invoke web services, not create them). To test it out, I decided to invoke one of Amazon.com's web services. To start out, I had to register to use their eCommerce web service. Then, I had to dig through many many links to actually find some documentation on how to use this stuff. The whole Amazon Web Service (AWS) section is a bit confusing to someone who has never used it before. It is hard to differentiate between examples and pre-built solutions and documentation.

Finally, I figured it out, and worked out this small example to search for a book with the title "Muscle: Confessions of an Unlikely BodyBuilder." This happens to be an amazing book and for anyone who loves to workout, this is a must-read!

But anyway, onto the code:

 Launch code in new window » Download code as text file »

  • <!---
  • Use the REST web service that uses traditional URL
  • invokation. In this methodology, the arguments for
  • the web service are sent as standard, encoded URL
  • arguments (essentially query string arguments).
  • --->
  • <cfhttp
  • url="http://webservices.amazon.com/onca/xml"
  • method="GET"
  • result="REQUEST.WSResponse">
  •  
  • <!--- Desired Amazon web service. --->
  • <cfhttpparam
  • type="URL"
  • name="Service"
  • value="AWSECommerceService"
  • />
  •  
  • <!--- Authorization ID for use of Amazon Web Services. --->
  • <cfhttpparam
  • type="URL"
  • name="AWSAccessKeyId"
  • value="xxxxxxxxxxxxxxxxxxxx"
  • />
  •  
  • <!---
  • Action to take on this web service. In this case,
  • we are going to be searching the product catalog.
  • --->
  • <cfhttpparam
  • type="URL"
  • name="Operation"
  • value="ItemSearch"
  • />
  •  
  • <!---
  • Searching must be done within a macro-category. In
  • this case, we are searching for books.
  • --->
  • <cfhttpparam
  • type="URL"
  • name="SearchIndex"
  • value="Books"
  • />
  •  
  • <!--- Search for books of this title. --->
  • <cfhttpparam
  • type="URL"
  • name="Title"
  • value="Muscle confessions bodybuilder"
  • />
  •  
  • </cfhttp>
  •  
  •  
  • <!---
  • Check to see if the request hit a valid page and that the
  • generated reponse is a valid XML document.
  • --->
  • <cfif (
  • Find( 200, REQUEST.WSResponse.StatusCode ) AND
  • IsXml( REQUEST.WSResponse.FileContent )
  • )>
  •  
  •  
  • <!---
  • We have gotten a valid response back from the web
  • service. Before we start to work with it, let's strip
  • out all XML name spaces. This will make my XPath
  • search queries much easier (HEY! I don't know that much
  • about XPath so get off my back).
  •  
  • We are stripping out any instance of something like:
  • xmlns="...."
  •  
  • This should create simple XML nodes.
  • --->
  • <cfset strXmlData = REQUEST.WSResponse.FileContent.ReplaceAll(
  • "\s*xmlns=""[^""]*""",
  • ""
  • ) />
  •  
  •  
  • <!---
  • Now that we have cleaned the XML data, let' parse it
  • into a ColdFusion XML document structure.
  • --->
  • <cfset xmlResults = XmlParse( strXmlData ) />
  •  
  • <!--- Get the item nodes. --->
  • <cfset arrItems = XmlSearch( xmlResults, "//Item" ) />
  •  
  • <!---
  • Get the error nodes. This will contain any problems
  • that occurred with the web service invokation.
  • --->
  • <cfset arrErrors = XmlSearch( xmlResults, "//Error" ) />
  •  
  •  
  • <!--- Check to see if we have any errors. --->
  • <cfif ArrayLen( arrErrors )>
  •  
  • <!---
  • Something has gone wrong with the web service.
  • Output these errors.
  • --->
  • <cfloop
  • index="intError"
  • from="1"
  • to="#ArrayLen( arrErrors )#"
  • step="1">
  •  
  • <h4>
  • #arrErrors[ intError ].Code.XmlText#
  • </h4>
  •  
  • <p>
  • #arrErrors[ intError ].Message.XmlText#
  • </p>
  •  
  • </cfloop>
  •  
  • <cfelse>
  •  
  • <!---
  • The web service request went off without a problem.
  • Now, all we have to do is list out the results.
  • --->
  • <table cellspacing="2" cellpadding="3" border="1">
  •  
  • <!--- Loop over items. --->
  • <cfloop
  • index="intItem"
  • from="1"
  • to="#ArrayLen( arrItems )#"
  • step="1">
  •  
  • <!--- Get a reference to this item. --->
  • <cfset xmlItem = arrItems[ intItem ] />
  •  
  • <tr>
  • <td colspan="2">
  • <strong>
  • #xmlItem.ItemAttributes.Title.XmlText#
  • </strong>
  • </td>
  • </tr>
  • <tr>
  • <td>
  • Author
  • </td>
  • <td>
  • #xmlItem.ItemAttributes.Author.XmlText#
  • </td>
  • </tr>
  • <tr>
  • <td>
  • ASIN
  • </td>
  • <td>
  • <a
  • href="#xmlItem.DetailPageURL.XmlText#"
  • target="_blank"
  • >#xmlItem.ASIN.XmlText#</a>
  • </td>
  • </tr>
  •  
  • </cfloop>
  • </table>
  •  
  • </cfif>
  •  
  •  
  • <cfelse>
  •  
  • <!---
  • Something went wrong with the web service request.
  • Either the server returned a non-200 response code or
  • the generated content was not in XML format. Output
  • the data for debugging.
  • --->
  • <p>
  • <em>There was a problem invoking the web service.</em>
  • </p>
  •  
  • <code>
  •  
  • #HtmlEditFormat(
  • REQUEST.WSResponse.FileContent
  • )#
  •  
  • </code>
  •  
  • </cfif>

This was my first introduction to REST-style web service invocation. I had seen the phrase REST thrown around here and there, but never really understood what it meant. After doing some quick Googling, it looks like REST is basically a no-methodology-methodology???? Basically, it uses standard HTTP to invoke web services. The XML response is just returned as part of the generated file content. Sorry if this is grossly incorrect... like I said, this was my first time trying to even look it up.

So, as you can see, I invoke the web service using ColdFusion's CFHttp tag. Each argument to the web service is passed as a URL CFHttpParameter. You could just as easily put these values as part of the query string in the URL attribute of the CFHttp tag, but this way creates a much nicer structure that will be easier to manipulate programmatically.

The rest of it is just being able to traverse XML documents and perform some simple XPath xml searches. One thing that I do that I would like to touch upon is the removing of all name space attributes from the XML nodes:

 Launch code in new window » Download code as text file »

  • <cfset strXmlData = REQUEST.WSResponse.FileContent.ReplaceAll(
  • "\s*xmlns=""[^""]*""",
  • ""
  • ) />

I do this because I am not very good at using XPath and ColdFusion's XmlSearch() method. The name space arguments seem to mess up the XPath search "//Item". However, if I go through and use a regular expression replace to strip out all xml node attributes that define a name space, the XPath works quite nicely. So, admittedly, this is a bit of a hack (I am altering the returned data), but it's the only way I know how to get it working. Plus, I couldn't care less about name spaces. I am not even sure yet what they are used for.

That's all there is to it. Quite easy. In fact, I found it was harder to find the web service definition than it was to eventually invoke it. And a final note: Please do not look at any of this as a BEST practice. This was my first time ever invoking a web service, so it's all trial and error.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Jul 24, 2008 at 10:47 PM // reply »
1 Comments

Thanks, used your code as example to make an amazon cfc to do a couple of REST calls and utilize the amazon webservice. Currently working on http://www.utpabooks.com using amazon as the backbone.


Oct 15, 2009 at 5:03 PM // reply »
2 Comments

Very cool post. I was curious if you had updated this to account for Amazons new authentication policy that went into place in August?


Oct 16, 2009 at 8:12 AM // reply »
7,572 Comments

@Mark,

To be honest, I have not looked at Amazon's web services since this post :)


Dec 8, 2009 at 3:50 PM // reply »
2 Comments

Please whats the difference between REST and SOAP? Why don't you use the normal <cfinvoke webservice=..........>??


Dec 8, 2009 at 5:08 PM // reply »
7,572 Comments

@Leonard,

REST and SOAP are just two different ways to invoke a web service. CFInvoke is no more normal than CFHTTP for web services. It just depends on what kind of technologies you have available. In fact, you can post to a SOAP web service using CFHTTP (which is probably why they have a CFHttpParam of type:xml).

REST uses URL or FORM parametrs. SOAP posts XML packets.

I personally find CFHTTP easier to use and debug.


BB
Jan 11, 2010 at 11:30 AM // reply »
2 Comments

hi, when I dump xml for example from Amazon's API, how do I access the values of the blue struct nodes?


Jan 11, 2010 at 12:53 PM // reply »
2 Comments

@BB,

Hi,

i have actually not used amazon's api before but you need to convert the xml to a coldfusion document object using XmlParse() function and use the XmlSearch() function to get values from each node.

Example: <cfset xmlRequest = XmlParse(strXml.Trim()) />

where strXml is the xml var returned

<cfset arrSearchNodes = XmlSearch(xmlRequest,"//node1/node2") />

where node1 is the root level of the xml object, node2 the next level and so on.

To retrieve values within the last node level

<cfoutput>#arrSearchNodes[1].firstName.XmlText#</cfoutput>

where firstName is the tag name of the xml returned

I also had problems removing soap from soap:nodeName but found a solution on one of Ben's articles http://www.bennadel.com/blog/494-Stripping-XML-Name-Spaces-And-Node-Prefixes-From-ColdFusion-XML-Data-To-Simplify-XPath-.htm

the article should provide u with as much information as possible cos i used it

hope it helps


BB
Jan 11, 2010 at 1:18 PM // reply »
2 Comments

@Leonard Inegbedion,

Hi thanks,

I got it finally with this. It returned "Translator"

tmp.ItemLookupResponse.Items[1].Item.ItemAttributes.Creator[1].XmlAttributes["Role"]


Jan 11, 2010 at 1:48 PM // reply »
7,572 Comments

@BB,

Glad you got it working. You can typically thing of XML documents as structures and arrays. It gets a bit complicated with node names with namespaces and embedded dots (as @Leonard eludes to).


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 21, 2010 at 6:32 AM
ColdFusion CFPOP - My First Look
Apologies... The field name in the db for C. is "BounceCode" It stores the code / message which is returned in the email. Sorry for the confusion. ... read »
Mar 21, 2010 at 6:29 AM
ColdFusion CFPOP - My First Look
@Jose Galdamez, Hi Ben and Jose 1st of all.. big thanks to Jose for his Skype chat a few weeks back. Your time was much appreciated. I have come up with a rather unelegant solution to my problem a ... read »
Mar 21, 2010 at 3:42 AM
A New Wrist Pain
Chiropractic treatment is one of the best methods for treating numerous health problems naturally. After years of experience being a chiropractor, I have found that it is a powerful way to solve many ... read »
Mar 20, 2010 at 12:07 PM
Drawing On The iPhone Canvas With jQuery And ColdFusion
Simply awesome. Saved my day. ... read »
Mar 20, 2010 at 9:00 AM
Building A Fixed-Position Bottom Menu Bar (ala FaceBook)
I would like to say thx for an easy way to create a bottom bar. I do have a ?. Is it possible to center the bar if i want to resize it to ex 85%. Regards Offenbach ... read »
Mar 19, 2010 at 7:26 PM
MySQL 3/4 - com.mysql.jdbc.Driver And allowMultiQueries=true
Thank you very much for this post. Adding allowMultiQueries="true" in context.xml didn't help until I added it to url as allowMultiQueries=true Good idea is to use prepared statements and it will he ... read »
Jim
Mar 19, 2010 at 4:49 PM
Nobody Puts Baby In The Corner!
Wow. This is like suddenly finding a support group for your secret shame. I'm not alone! I always liked this movie, even though it is extremely cheesy. I just wish Jennifer Grey hadn't gotten the ... read »
Mar 19, 2010 at 4:47 PM
Application.cfc OnRequest() Method Affects OnError() Arguments
@Jason and @Ben, I've been doing some CF9 refactoring on our systems and noticed an odd occurrence with onError as well. Found a way to work around my problem, but what I saw was... Background: Our ... read »