Nav Bar

lunes, 1 de abril de 2013

Generating an API

Afternoon mates. Today I'm starting to develop Quived's API. It's going to be used by smartphones or tablets which have the app installed. It's very important to bare in mind the different HTTP methods when doing this. These codes are the base in which the API sits. In order to know when a request has been successfull or not, you exchange different HTTP status codes.
As always, wikipedia is a perfect place to find information about this: link
More specifically speaking, when doing this in a grails application. Is very important to set up the path that the URL mapping is going to follow. We set this in the URLMappings.groovy file. Inserting the following code for each new route you want to define:

"/rest/url/**" (controller:"YourRestController", action:"method") {
            action = [GET: "method"];
        }


One thing I found out while doing this, is that you are not supposed to put the entire: controllerNameController class name, just the name itself: controllerName.

Once the route to our API method is set, we can define the logic we want it to deliver. Regarding HTTP statuses, in grails we do as follow (at least I do):

  • response.status = 200; // 200 OK. Just to set the status.
  • render(status:204, text:'Extra information about the status') // 204 NO CONTENT. Status + information
I hope this helps.

Cheers!