How to call web services with REST instead of SOAP

by Jeff Webb

Related link:

With names like SOAP and REST, you'd expect a couple cute jokes here. Perish the thought; I promised code in my last post and code you shall have.
The VB/VBA tools and samples from Microsoft demonstrate calling web services through SOAP interfaces. For example, the Office Web Services Toolkit generates proxy classes for web services using SOAP. You then use those proxy classes in your local VBA code to call the remote web service.
That works fine as long as you don't try to modify the proxy classes to, the web service asynchonously. Modifying generated code is often more difficult than writing (and thus understanding) the code yourself.
The Web Services Toolkit also generates proxy classes for every item described by the web service's WSDL -- in the case of the Amazon web service that's dozens of classes. It's hard to figure out which ones you need and cumbersome to use this overly object-oriented approach.
Good news! Some web services, like Amazon, provide an alternate, string-based interface. This interface looks like query strings (heck, that's what it is really) but it also has a formal name: REST.
To see how it works, click on the following web address:'wombat'
OK, you got a bunch of XML about wombats. That's useful from a programming perspective because the interface is so much simpler than using the Web Services Toolkit and SOAP.
Lookee here:

Sub VBCode()
' Requires reference to Microsoft XML object library.
Dim xdoc As New DOMDocument, search As String
' Create the search request
search = "" & _
"?t=" & "webservices-20" & _
"&dev-t=" & "D1UCR04XBIF4A6" & _
"&page=1" & _
"&f=xml" & _
"&mode=books" & _
"&type=lite" & _
xdoc.Load search
' Display the results
Debug.Print xdoc.XML
End Sub

That code just displays the results in the Immediate window, but you can just as easily display those results in a document through an XML Map you set up previously. For instance:

Set wb = ThisWorkbook
wb.XmlImportXml xdoc.XML, wb.XmlMaps("AMZN_Map"), True

Voila! You've gotten your results in six lines or less.
The other advantage of this interface is that it's easy to use asynchronously. If you set the DOMDocument object's Async property to True, you can then hook into the ondataavailable event as seen here:

' Move the xdoc declaration to the module level.
Dim WithEvents xdoc As DOMDocument

Private Sub xdoc_ondataavailable()
Set wb = ThisWorkbook
wb.XmlImportXml xdoc.XML, wb.XmlMaps("AMZN_Map"), True
End Sub

So why doesn't Microsoft show REST examples? I don't know, but you should be aware that not all web services have REST interfaces. Google doesn't, and that's a complaint among some web programmers. On the other hand, SQL Server's XML support through IIS looks like REST. So you decide.