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.