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)
October 20th, 2008 at 12:10 pm
How do you deploy this restful service on apache httpd? I coded the sample and compiled but now what?.
October 20th, 2008 at 12:30 pm
Hi Cheeke,
Axis2/C has a httpd module that allows you to deploy axis2/c services in httpd. Here is a detailed documentation on that, http://ws.apache.org/axis2/c/docs/installationguide.html#3.
Anyway first of all if you have the service compiled, test the service using axis2_http_server and make sure it is working correctly.
Thanks
Dimuthu
October 20th, 2008 at 12:39 pm
Thanks a lot Dimuthu.
Your article is very helpful.
October 20th, 2008 at 3:17 pm
Here is a demo codes of the application I used in the blog, http://downloads.dimuthu.org/codes/axis2/school.zip
Note that here the service is not responding with real values, but just a description of what operation called with what parameters..
You can access each operation by urls like (localhost should be renamed to your host).
http://localhost/axis2/services/schoool/students
http://localhost/axis2/services/schoool/subjects
http://localhost/axis2/services/schoool/students/james
http://localhost/axis2/services/schoool/subjects/maths
http://localhost/axis2/services/schoool/students/james/marks/maths
Note: it is recently reported in the latest code when accessing urls with firefox it doesn’t respond due to the request content type, So please try out with an Axis2/Client or Some other browser like IE.
Thanks
Dimuthu
November 21st, 2008 at 10:26 am
[...] weeks back I wrote a blog post about Writing RESTful Services in C which explain the use of Axis2/C REST API. Basically when you provide a HTTP Method (GET, POST, PUT [...]