Write 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.

This entry was posted in REST, RESTful, Tutorial/Guide, web services and tagged , , , , , . Bookmark the permalink.

8 Responses to Write RESTful Services in C

  1. Cheeke says:

    How do you deploy this restful service on apache httpd? I coded the sample and compiled but now what?.

  2. dimuthu says:

    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

  3. Cheeke says:

    Thanks a lot Dimuthu.

    Your article is very helpful.

  4. dimuthu says:

    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

  5. Pingback: Apache Axis2/C RESTful URL Mapping Algorithm | Dimuthu's Blog

  6. 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

  7. dimuthu says:

    Hi,
    What is the axis2 version you are using?. Probably an issue in a latest pack?

    Thanks
    Dimuthu

  8. I’m using Axis2/C 1.6.0 compiled from source. I tried echo_rest example and via curl and got the same results….

Leave a Reply

Your email address will not be published. Required fields are marked *