Kushal Das

FOSS and life. Kushal Das talks here.

How to do XMLRPC calls in Vala using libsoup (tutorial)

This is small tutorial showing how one can do XMLRPC in Vala using libsoup. We will call a default demo.sayHello method in Wordpress, this will just return a string “Hello!”.

The code:

using Soup;

public void main(string[] args) { var message = xmlrpc_request_new(“http://kushaldas.wordpress.com/xmlrpc.php","demo.sayHello"); var session = new SessionSync(); session.send_message(message);

    string data = message.response_body.flatten().data;
    Value v = Value(typeof(string));
    try {
        xmlrpc_parse_method_response(data, -1,v);
    }
    catch(Error e) {
        debug("Error while processing the response");    
    }
    string msg = v.get_string();
    debug("Got: %s\n", msg);

}

At the very first line we mention that we are using the libsoup, then using xmlrpc_request_new method we create a message. It requires the URL and method name strings. It also takes optional arguments to the method (which we will see in the next example). Then in the next few lines we are creating a SessionSync object and call the method using send_message method. The response stays in the same message object we created before. We get the response as string in the variable data.

xmlrpc_parse_method_response takes the response as a string in the first argument, next is the length(size) of the response (Note: I used -1 so that it will take the size of data, it comes handy when the response contains unicode chars) and 3rd argument is the Value where it will store the parsed result. string msg = v.get_string() gives us the final result , which we print using debug.

Now to compile the code I gave the following command $ valac --pkg libsoup-2.4 --thread xmlrpc-test.vala The output $ ./xmlrpc-test ** (process:28319): DEBUG: xmlrpc-test.vala:17: Got: Hello!

In the next example we will try to add two numbers using another demo method given by wordpress. The code is given below

using Soup;

public void main(string[] args) { var message = xmlrpc_request_new(“http://kushaldas.wordpress.com/xmlrpc.php","demo.addTwoNumbers",typeof(int),20,typeof(int),30); var session = new SessionSync(); session.send_message(message);

    string data = message.response_body.flatten().data;
    Value v = Value(typeof(int));
    try {
        xmlrpc_parse_method_response(data, -1,v);
    }
    catch(Error e) {
        debug("Error while processing the response");    
    }
    int msg = v.get_int();
    debug("Got: %d\n", msg);

}

Here while creating the message, we passed the required arguments to the method demo.addTwoNumbers, it goes as type and value pair, so we wrote typeof(int) and then the integer value.

Compilation and result

[kdas@d80 vala]$ valac --pkg libsoup-2.4 xmlrpc-test2.vala --thread [kdas@d80 vala]$ ./xmlrpc-test2 ** (process:28565): DEBUG: xmlrpc-test2.vala:17: Got: 50

A small tip: If you don’t know the return type , use type_name() to find out. Thanks to the people in #vala who are continuously helping me to understand more of it.

The post is brought to you by lekhonee v0.8

comments powered by Disqus