XML-RPC

9/9/02


XML-RPC is known as a web service.  Web services are a set of tools that let one build distributed applications on top of existing web infrastructures.  These applications use the Web as a kind of "transport layer" but don't offer a direct human interface via the browser.[1]  Extensible Markup Language (XML) provides a vocabulary for describing Remote Procedure Calls (RPC), which is then transmitted between computers using the HyperText Transfer Protocol (HTTP).  Effectively, RPC gives developers a mechanism for defining interfaces that can be called over a network.  These interfaces can be as simple as a single function call or as complex as a large API. 

XML-RPC therefore allows two or more computers running different operating systems and programs written in different languages to share processing.  For example, a Java application could talk with a Perl program, which in turn talks with Python application that talks with ASP, and so on.  System integrators often build custom connections between different systems, creating their own formats and protocols to make communications possible, but one can often end up with a large number of poorly documented single-use protocols.  The RPC approach spares programmers the trouble of having to learn about underlying protocols, networking, and various implementation details. 

XML-RPC can be used with Python, Java, Perl, PHP, C, C++, Ruby, Microsoft’s .NET and many other programming languages.  Implementations are widely available for platforms such as Unix, Linux, Windows and the Macintosh. 

An XML-RPC call is conducted between two parties: the client (the calling process) and the server (the called process).  A server is made available at a particular URL (such as http://example.org:8080/rpcserv/).

Here is a short XML-RPC client written in Python:

[ccrow@dhcp-86 ccrow]$ python
Python 2.2.1 (#1, Apr 9 2002, 13:10:27)
[GCC 2.96 20000731 (Red Hat Linux 7.1 2.96-98)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import xmlrpclib
>>> server = xmlrpclib.Server("http://betty.userland.com/RPC2")
>>> server.examples.getStateName(30)
'New Jersey'




To illustrate XML-RPC's flexibility, the follow is the same client written in Perl:

use Frontier::Client;
$server = Frontier::Client->new(url => 'http://betty.userland.com/RPC2');
$name = $server->call('examples.getStateName', 30);
print "$name\n";



An XML-RPC message is an HTTP-POST request.  The body of the request is in XML.  A procedure executes on the server and the value it returns is also formatted in XML. 

For example, the XML-RPC client's request XML document from the above code (the code that is send to the server over HTTP) is:

POST /RPC2 HTTP/1.0
Host: betty.userland.com
User-Agent: xmlrpclib.py/1.0.0 (by www.pythonware.com)
Content-Type: text/xml
Content-Length: 161
<?xml version='1.0'?>
<methodCall>
   <methodName>examples.getStateName</methodName>
   <params>
      <param>
         <value><int>30</int></value>
      </param>
   </params>
</methodCall> 

 

In the first line of the request, the HTTP POST method indicates to the host server that the client is sending it data.  The path (/RPC2) following the POST method indicates the URL, relative to the server, of the script that should receive the data.  The path of /RPC2 is commonly used in various XML-RPC implementations, but there is no special need for a server to be placed at any particular path.[2]

The Host header specifies the name of the server that should service this XML-RPC request.  Borrowed from HTTP 1.1, the Host header allows the use of a virtual server that shares the same IP address as other servers. 

The value of the Content-Type header is constant within XML-RPC; it is the Internet media type for XML, text/xml.  The User-Agent header carries an identifier corresponding to the XML-RPC implementation that one uses.  It is normally of little significance and will never be observed if one develops with ready-made XML-RPC libraries for the specific platform of choice.[3]

The XML-RPC server's reply (also an XML document) is:

HTTP/1.1 200 OK
Connection: close
Content-Length: 139
Content-Type: text/xml
Date: Thu, 22 Aug 2002 19:52:20 GMT
Server: UserLand Frontier/7.0.1-WinNT
<?xml version="1.0"?>
<methodResponse>
   <params>
      <param>
         <value>New Jersey</value>
      </param>
   </params>
</methodResponse>
 

 

Any data item in an XML-RPC request or response is contained within a <value>…</value> element.  XML-RPC defines a set of simple data types, from which other, more complex, data structures can be built.  These types are common to most programming languages.  Procedure parameters can be scalars, numbers, strings, dates, compound structures, to name a few.  If no additional tags are inserted within the <value>…</value> element (as in the above XML document), the data is assumed to be the string type.

A <struct> contains <member>s and each <member> contains a <name> and a <value>.  Below is an example of a two-element <struct>:


<struct>
   <member>
      <name>lowerBound</name>
      <value><i4>18</i4></value>
   </member>
   <member>
      <name>upperBound</name>
      <value><i4>139</i4></value>
   </member>
</struct>

 

Furthermore, <struct>’s can be recursive and any <value> may contain a <struct> or any other type, including an <array>.

 

OTHER DISTRIBUTED COMPUTING PROTOCOLS

XML-RPC is not the only way to make remote procedure calls. Other popular protocols include CORBA, DCOM and SOAP.  Each of these protocols has advantages and disadvantages.

The Simple Object Access Protocol (SOAP) is another much-hyped protocol for implementing web services.  SOAP is very similar to XML-RPC, except SOAP is firmly rooted in the object-oriented paradigm.  SOAP passes live objects, each with their own state information, across the network.  Imagine a Python SOAP server that creates a customer object that can be modified by a Perl or Java client.  It works as XML-RPC by marshaling procedure calls over HTTP as XML documents.  While XML-RPC provides fewer capabilities than SOAP, it also has far fewer interoperability problems, and its capabilities and limitations are much better understood.  XML-RPC is also stable, with over 30 implementations on a wide variety of platforms.

CORBA is a popular protocol for writing distributed, object-oriented applications and is well-supported by many vendors and several free software projects.  CORBA works well with Java and C++, and is available for many other languages.  It also provides a powerful interface definition language (IDL), allowing one to define readable, object-oriented APIs.  Unfortunately, as a result, CORBA is very complex.  It has a steep learning curve, requires significant effort to implement, and fairly sophisticated clients.

DCOM is Microsoft's answer to CORBA.  Previously called "Network OLE," DCOM is designed for use across multiple network transports, including Internet protocols such as HTTP.  DCOM is based on the Open Software Foundation's DCE-RPC specifications and will work with both Java applets and ActiveX® components through its use of the Component Object Model (COM).[4]  DCOM becomes useless, though, if one does not use COM components (or non-Microsoft systems).




The above text just touches the surface of XML-RPC.  I recommend O'Reilly's "Programming Web Service with XML-RPC" for further reading.  One may also wish to review the following links:

XML-RPC Home Page
XML-RPC for C and C++
The Apache XML-RPC Project
Expat: The XML Parser


 

[1] “Programming Web Services with XML-RPC”, O’Reilly, p.1
[2] “Programming Web Services with XML-RPC”, O’Reilly, p.22
[3] “Programming Web Services with XML-RPC”, O’Reilly, p.24
[4] http://www.microsoft.com/com/tech/DCOM.asp

 

back