XML-RPC - Introduction

RPC stands for Remote Procedure Call. As its name indicates, it is a mechanism to call a procedure or a function available on a remote computer. RPC is a much older technology than the Web. 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.

What is XML-RPC ?

XML-RPC is among the simplest and most foolproof web service approaches that makes it easy for computers to call procedures on other computers.

Why XML-RPC ?

If you need to integrate multiple computing environments, but don't need to share complex data structures directly, you will find that XML-RPC lets you establish communications quickly and easily.

Even if you work within a single environment, you may find that the RPC approach makes it easy to connect programs that have different data models or processing expectations and that it can provide easy access to reusable logic.

XML-RPC Technical Overview

XML-RPC consists of three relatively small parts:

We will study all these three components in the next three chapters.

XML-RPC - Data Model

The XML-RPC specification defines six basic data types and two compound data types that represent combinations of types.

Basic Data Types in XML-RPC

These basic types are always enclosed in value elements. Strings (and only strings) may be enclosed in a value element but omit the string element. These basic types may be combined into two more complex types, arrays, and structs. Arrays represent sequential information, while structs represent name-value pairs, much like hashtables, associative arrays, or properties.

Arrays are indicated by the array element, which contains a data element holding the list of values. Like other data types, the array element must be enclosed in a value element. For example, the following arraycontains four strings:

   This  is  an  array.    

The following array contains four integers:

   7 1247 -91 42    

Arrays can also contain mixtures of different types, as shown here:

   1 Chaotic collection, eh? -91 42.14159265    

Creating multidimensional arrays is simple - just add an array inside of an array:

      10 20 30      15 25 35       

A simple struct might look like:

   givenName Joseph  familyName DiNardo  age 27    

This way you can implement almost all data types supported by any programming language.

XML-RPC - Request Format

XML-RPC requests are a combination of XML content and HTTP headers. The XML content uses the data typing structure to pass parameters and contains additional information identifying which procedure is being called, while the HTTP headers provide a wrapper for passing the request over the Web.

Each request contains a single XML document, whose root element is a methodCall element. Each methodCall element contains a methodName element and a params element. The methodName element identifies the name of the procedure to be called, while the params element contains a list of parameters and their values. Each params element includes a list of param elements which in turn contain value elements.

For example, to pass a request to a method called circleArea, which takes a Double parameter (for the radius), the XML-RPC request would look like:

  circleArea  2.41    

The HTTP headers for these requests will reflect the senders and the content. The basic template looks as follows:

POST /target HTTP 1.0 User-Agent: Identifier Host: host.making.request Content-Type: text/xml Content-Length: length of request in bytes

For example, if the circleArea method was available from an XML-RPC server listening at /xmlrpc, the request might look like:

POST /xmlrpc HTTP 1.0 User-Agent: myXMLRPCClient/1.0 Host: 192.168.124.2 Content-Type: text/xml Content-Length: 169

Assembled, the entire request would look like:

POST /xmlrpc HTTP 1.0 User-Agent: myXMLRPCClient/1.0 Host: 192.168.124.2 Content-Type: text/xml Content-Length: 169  circleArea  2.41    

It's an ordinary HTTP request, with a carefully constructed payload.

XML-RPC - Response Format

Responses are much like requests, with a few extra twists. If the response is successful - the procedure was found, executed correctly, and returned results - then the XML-RPC response will look much like a request, except that the methodCall element is replaced by a methodResponse element and there is no methodName element:

    18.24668429131    

Like requests, responses are packaged in HTTP and have HTTP headers. All XML-RPC responses use the 200 OK response code, even if a fault is contained in the message. Headers use a common structure similar to that of requests, and a typical set of headers might look like:

HTTP/1.1 200 OK Date: Sat, 06 Oct 2001 23:20:04 GMT Server: Apache.1.3.12 (Unix) Connection: close Content-Type: text/xml Content-Length: 124

A complete response, with both headers and a response payload, would look like:

HTTP/1.1 200 OK Date: Sat, 06 Oct 2001 23:20:04 GMT Server: Apache.1.3.12 (Unix) Connection: close Content-Type: text/xml Content-Length: 124    18.24668429131    

After the response is delivered from the XML-RPC server to the XML-RPC client, the connection is closed. Follow-up requests need to be sent as separate XML-RPC connections.

XML-RPC - Fault Format

XML-RPC faults are a type of responses. If there was a problem in processing a XML-RPC request, the methodResponse element will contain a fault element instead of a params element. The fault element, like the params element, has only a single value that indicates something went wrong. A fault response might look like:

   No such method!   

A fault will also have an error code. XML-RPC doesn't standardize error codes at all. You'll need to check the documentation for particular packages to see how they handle faults.

A fault response could also look like:

      code 26  message No such method!      

XML-RPC - Examples

To demonstrate XML-RPC, we're going to create a server that uses Java to process XML-RPC messages, and we will create a Java client to call procedures on that server.

The Java side of the conversation uses the Apache XML Project's Apache XML-RPC, available at http://xml.apache.org/xmlrpc/

Put all the .jar files in appropriate path and let us create one client and one small XML-RPC server using JAVA.

XML-RPC Client

Let us write an XML-RPC client to call a function called sum function. This function takes two parameters and returns their sum.

import java.util.*; import org.apache.xmlrpc.*; public class JavaClient < public static void main (String [] args) < try < XmlRpcClient client = new XmlRpcClient("http://localhost/RPC2"); Vector params = new Vector(); params.addElement(new Integer(17)); params.addElement(new Integer(13)); Object result = server.execute("sample.sum", params); int sum = ((Integer) result).intValue(); System.out.println("The sum is: "+ sum); >catch (Exception exception) < System.err.println("JavaClient: " + exception); >> >

Let us see what has happened in the above example client.