Examples
Client example
Below is a small sample code displaying the use of the library in a client application.
require "xmlrpc.http" local ok, res = xmlrpc.http.call ( "http://www.oreillynet.com/meerkat/xml-rpc/server.php", "system.listMethods") print (ok) for i, v in pairs(res) do print ('\t', i, v) end
Type conversion example
The next example shows how to force the conversion of types from Lua to XML-RPC.
require "xmlrpc" double_array_type = xmlrpc.newArray ("double") double_array = xmlrpc.newTypedValue ( { 1.1, 2, 3, 4 }, double_array_type) double_array_array_type = xmlrpc.newArray (double_array_type) double_array_array = xmlrpc.newTypedValue ( { { 11, 12, 13, }, { 21, 22, 23, }, { 31, 32, 33, }, }, double_array_array_type)
The table double_array_array
will be:
<array> <data> <value> <array> <data> <value><double>11</double></value> <value><double>12</double></value> <value><double>13</double></value> </data> </array> </value> <value> <array> <data> <value><double>21</double></value> <value><double>22</double></value> <value><double>23</double></value> </data> </array> </value> <value> <array> <data> <value><double>31</double></value> <value><double>32</double></value> <value><double>33</double></value> </data> </array> </value> </data> </array>
Server example
Follows a small example of a server based on a cgi launcher.
require "xmlrpc.cgi" local kepler_home = "http://www.keplerproject.org" local kepler_products = { "luasql", "lualdap", "luaexpat", "luaxmlrpc", } local kepler_sites = { luasql = kepler_home.."/luasql", lualdap = kepler_home.."/lualdap", luaexpat = kepler_home.."/luaexpat", luaxmlrpc = kepler_home.."/luaxmlrpc", } -- Register methods xmlrpc.srvMethods { kepler = { products = function (self) return kepler_products end, site = function (self, prod) return kepler_sites[prod] end, } } -- Parse POST data local doc = {} post.parsedata (doc) -- Decode method call local method, arg_table = xmlrpc.srvDecode (doc[1]) local func = xmlrpc.dispatch (method) local ok, result, err = pcall (func, unpack (arg_table or {})) if ok then result = { code = 3, message = result, } end respond (xmlrpc.srvEncode (result, not ok))
Note that the package post
and the function
respond
should be provided by the cgi launcher.