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 Xavante
WSAPI. You can call the service for example with
the xmlrpc tool
from XML-RPC
C (xmlrpc http://localhost:12345
hello_world) or by
using examples/client.lua in the source
tarball.
require("xavante")
require("xavante.httpd")
require("wsapi.xavante")
require("wsapi.request")
require("xmlrpc")
--- XML-RPC WSAPI handler
-- @param wsapi_env WSAPI environment
function wsapi_handler(wsapi_env)
local headers = { ["Content-type"] = "text/xml" }
local req = wsapi.request.new(wsapi_env)
local method, arg_table = xmlrpc.srvDecode(req.POST.post_data)
local func = xmlrpc.dispatch(method)
local result = { pcall(func, unpack(arg_table or {})) }
local ok = result[1]
if not ok then
result = { code = 3, message = result[2] }
else
table.remove(result, 1)
if table.getn(result) == 1 then
result = result[1]
end
end
local r = xmlrpc.srvEncode(result, not ok)
headers["Content-length"] = tostring(#r)
local function xmlrpc_reply(wsapienv)
coroutine.yield(r)
end
return 200, headers, coroutine.wrap(xmlrpc_reply)
end
-- XML-RPC exported functions
xmlrpc_exports = {}
--- Get simple string.
-- @return simple string
function xmlrpc_exports.hello_world()
return "Hello World"
end
local rules = {{ match = ".", with = wsapi.xavante.makeHandler(wsapi_handler) }}
local config = { server = {host = "*", port = 12345}, defaultHost = { rules = rules} }
xmlrpc.srvMethods(xmlrpc_exports)
xavante.HTTP(config)
xavante.run()
Note that the package post and the function
respond should be provided by the cgi launcher.