October 18th, 2008Write RESTful Services in C
You can write REST as well as SOAP web services using Apache Axis2/C web services framework. There you can make existing Axis2/C web services RESTful just by providing the URL patterns and the HTTP methods to each operation inĀ the services.xml which act as a simple descriptor for an Axis2/C service.
So if we rewrite the RESTful Demo (Written in PHP) using Axis2/C, the services.xml would be something like following.
<service name="RESTfulSchool"> <!-- mentioning the service library--> <parameter name="ServiceClass" locked="xsd:false">RESTfulSchool</parameter> <!-- some description --> <description> The RESTful School demo </description> <!-- list of operations --> <operation name="getSubjects"> <parameter name="RESTMethod">GET</parameter> <parameter name="RESTLocation">subjects</parameter> </operation> <operation name="getSubjectInfoPerName"> <parameter name="RESTMethod">GET</parameter> <parameter name="RESTLocation">subjects/{name}</parameter> </operation> <operation name="getStudents"> <parameter name="RESTMethod">GET</parameter> <parameter name="RESTLocation">students</parameter> </operation> <operation name="getStudentInfoPerName"> <parameter name="RESTMethod">GET</parameter> <parameter name="RESTLocation">students/{name}</parameter> </operation> <operation name="getMarksPerSubjectPerStudent"> <parameter name="RESTMethod">GET</parameter> <parameter name="RESTLocation">students/{student}/marks/{subject}</parameter> </operation> </service>
We will check how to write the service logic for a operation like “getMarksPerSubjectPerStudent”.
axiom_node_t * RESTfulSchool_getMarksPerSubjectPerStudent( const axutil_env_t * env, axiom_node_t * request_payload) { axiom_node_t *student_node = NULL; axiom_node_t *subject_node = NULL; /* Extracting out the child nodes from the request */ student_node = axiom_node_get_first_child(request_payload, env); subject_node = axiom_node_get_next_sibling(student_node, env); /* now we can write the logic to retrieve the marks for the given student and subject and build and return the response payload */ return response_payload; }
As you can see the variables {student} and {subject} given in the services.xml can be easily accessed from your business logic, so we can build the response accordingly.
This way you can build a RESTful web services easily using C language.
![[Ask]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/ask.png)
![[Bloglines]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/bloglines.png)
![[del.icio.us]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/delicious.png)
![[Digg]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/digg.png)
![[diigo]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/diigo.png)
![[dzone]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/dzone.png)
![[Facebook]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/facebook.png)
![[Google]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/google.png)
![[MySpace]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/myspace.png)
![[MyWeb]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/myweb.png)
![[Newsvine]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/newsvine.png)
![[PlugIM]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/plugim.png)
![[Reddit]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/reddit.png)
![[Slashdot]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/slashdot.png)
![[Spurl]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/spurl.png)
![[StumbleUpon]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/stumbleupon.png)
![[Twitter]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/twitter.png)
![[Windows Live]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/windowslive.png)
![[Yahoo!]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/yahoo.png)
![[Email]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/email.png)