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.
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 [...]
November 21st, 2009 at 1:34 pm
Hi Dimuthu
Very nice article, however wnn I tried to use DELETE HTTP_METHOD I got “Internal sevrer error (500)” from the httpd. I took a quick look in the code for “axis2_http_transport_util_process_delete_request” and it looks that it fails because the operation is not set in msg_ctx. So what I’m missing? Did somebody see it working? I beleve so -:)
Thanks
Ilia
November 24th, 2009 at 3:16 am
Hi,
What is the axis2 version you are using?. Probably an issue in a latest pack?
Thanks
Dimuthu
November 24th, 2009 at 5:36 am
I’m using Axis2/C 1.6.0 compiled from source. I tried echo_rest example and via curl and got the same results….