XML RPC Tutorial
XML RPC Tutorial
Audience
This brief tutorial will be extremely useful for all those who want to learn how to
use XML-RPC to establish connections between computers across a network.
Prerequisites
XML-RPC is very easy to learn and use. You can make good use of this tutorial,
provided you have some exposure to XML vocabulary.
All the content and graphics published in this e-book are the property of
Tutorials Point (I) Pvt. Ltd. The user of this e-book is prohibited to reuse, retain,
copy, distribute or republish any contents or a part of contents of this e-book in
any manner without written consent of the publisher.
We strive to update the contents of our website and tutorials as timely and as
precisely as possible, however, the contents may contain inaccuracies or errors.
Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the accuracy,
timeliness or completeness of our website or its contents including this tutorial.
If you discover any errors on our website or in this tutorial, please notify us at
contact@tutorialspoint.com
i
Table of Contents
About the Tutorial ..................................................................................................................................... i
Audience .................................................................................................................................................... i
Prerequisites .............................................................................................................................................. i
1. INTRODUCTION..................................................................................................................... 1
What is XML-RPC?..................................................................................................................................... 1
5. FAULT FORMAT................................................................................................................... 11
6. EXAMPLES ........................................................................................................................... 13
7. SUMMARY .......................................................................................................................... 17
ii
XML - RPC
1. 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.
XML-RPC parameters are a simple list of types and content - structs and
arrays are the most complex types available.
With XML-RPC and web services, however, the Web becomes a collection
of procedural connections where computers exchange information along
tightly bound paths.
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.
1
XML - RPC
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.
We will study all these three components in the next three chapters.
2
XML - RPC
2. DATA MODEL
The XML-RPC specification defines six basic data types and two compound data
types that represent combinations of types.
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
3
XML - RPC
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 array contains four
strings:
<value>
<array>
<data>
<value><string>This </string></value>
<value><string>is </string></value>
<value><string>an </string></value>
<value><string>array.</string></value>
</data>
</array>
</value>
<value>
<array>
<data>
<value><int>7</int></value>
<value><int>1247</int></value>
<value><int>-91</int></value>
<value><int>42</int></value>
</data>
</array>
</value>
4
XML - RPC
<value>
<array>
<data>
<value><boolean>1</boolean></value>
<value><string>Chaotic collection, eh?</string></value>
<value><int>-91</int></value>
<value><double>42.14159265</double></value>
</data>
</array>
</value>
<value>
<array>
<data>
<value>
<array>
<data>
<value><int>10</int></value>
<value><int>20</int></value>
<value><int>30</int></value>
</data>
</array>
</value>
<value>
<array>
<data>
<value><int>15</int></value>
<value><int>25</int></value>
<value><int>35</int></value>
</data>
</array>
</value>
5
XML - RPC
</data>
</array>
</value>
<value>
<struct>
<member>
<name>givenName</name>
<value><string>Joseph</string></value>
</member>
<member>
<name>familyName</name>
<value><string>DiNardo</string></value>
</member>
<member>
<name>age</name>
<value><int>27</int></value>
</member>
</struct>
</value>
This way you can implement almost all data types supported by any
programming language.
6
XML - RPC
3. 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.
<?xml version="1.0"?>
<methodCall>
<methodName>circleArea</methodName>
<params>
<param>
<value><double>2.41</double></value>
</param>
</params>
</methodCall>
The HTTP headers for these requests will reflect the senders and the content.
The basic template looks as follows:
7
XML - RPC
For example, if the circleArea method was available from an XML-RPC server
listening at /xmlrpc, the request might look like:
8
XML - RPC
4. 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:
<?xml version="1.0"?>
<methodResponse>
<params>
<param>
<value><double>18.24668429131</double></value>
</param>
</params>
</methodResponse>
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
9
XML - RPC
XML-RPC only requires HTTP 1.0 support, but HTTP 1.1 is compatible.
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
<?xml version="1.0"?>
<methodResponse>
<params>
<param>
<value><double>18.24668429131</double></value>
</param>
</params>
</methodResponse>
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.
10
XML - RPC
5. FAULT FORMAT
<?xml version="1.0"?>
<methodResponse>
<fault>
<value><string>No such method!</string></value>
</fault>
</methodResponse>
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.
<?xml version="1.0"?>
<methodResponse>
<fault>
<value>
<struct>
<member>
<name>code</name>
<value><int>26</int></value>
</member>
<member>
<name>message</name>
<value><string>No such method!</string></value>
</member>
</struct>
11
XML - RPC
</value>
</fault>
</methodResponse>
12
XML - RPC
6. EXAMPLES
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 at 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.*;
13
XML - RPC
Note that all the parameters of the procedure call are always collected in a
Vector.
Note that the result of the remote procedure call is always an Object and
it has to be casted to the appropriate type.
Due to the above call, a client sends the following message to the server. Note
that this is handled by server.execute(...) internally and you have nothing to do
with it.
<value><int>17</int></value>
</param>
<param>
<value><int>13</int></value>
</param>
</params>
</methodCall>
XML-RPC Server
Following is the source code of XML-RPC Server written in Java. It makes use of
built-in classes available in org.apache.xmlrpc.*
import org.apache.xmlrpc.*;
For the call mentioned in the given example client, the server sends the
following response back to the client:
Now your server is ready, so compile and run it at your prompt as follows:
C:\ora\xmlrpc\java>java JavaServer
Attempting to start XML-RPC Server...
Started successfully.
Accepting requests. (Halt program to stop.)
C:\ora\xmlrpc\java>java JavaClient
30
16
XML - RPC
7. SUMMARY
In this tutorial, you have learnt what is XML-RPC and why do we need XML-RPC.
We have discussed about its data model as well as the request and response
message format to be exchanged between the client and the server. We have
given one example to demonstrate how XML-RPC client and server work to
exchange information.
While XML-RPC is simple, the creative application of simple tools can create
sophisticated and powerful architectures. In cases where a wide variety of
different systems need to communicate, XML-RPC may be the most appropriate
lowest common denominator.
What's Next?
The next step is to learn WSDL and SOAP.
WSDL
WSDL is an XML-based language for describing Web services and how to access
them.
WSDL describes a web service, along with the message format and protocol
details for the Web service.
If you want to learn more about WSDL, please go through our WSDL tutorial.
SOAP
SOAP is a simple XML-based protocol that allows applications to exchange
information over HTTP.
If you want to learn more about SOAP, please go through our SOAP tutorial.
17