Monday, January 23, 2012

REST - Taking a Closer Look (Part 3)

In the previous posts, we've discussed the general project, defined the concept of REST and taken a look at how the list or read operation.  Now as promised in REST - Taking a Closer Look (Part 2) we'll look at the POST method or create operation.

After clicking the 'Create Book' button, a form for the attributes about the book is displayed.  After entering the required values, we can click the 'Create' button.

Create Book Form
After clicking the 'Create' button the new book properties will be sent to the server and stored in the database.  The following screenshot details the request/response transaction from the browser with the client.  

Save Book Transition

The important pieces to note are:
  • Request URI is http://localhost:8080/research-grails-rest/book/save.
  • Request method is POST.
  • Response status code is 302.
  • Response location is http://localhost:8080/research-grails-rest/book/show/1.
The rest of the request and response headers are standard protocol speak.  The ones identified above indicate exactly how the data was saved and how the following response to the show state.

We have now seen three URIs in the transition between book listing, saving and showing a book.  The list operation is done with the GET method and the URI was formed as, http://localhost:8080/research-grails-rest/book/list.  The save operation is done using the POST method and the URI was, http://localhost:8080/research-grails/rest/book/save.  Lastly, the show operation also used a GET method, however specified an id to identify one of the items in the collection like so, http://localhost:8080/research-grails-rest/show/1.  

What we have seen so far is the basic blocks to working with a single resource.  Later we'll dig in further to understand the request model for updating, deleting and multiple items in the collection.  Next we'll take a closer look at REST and put a little more definition around what it is.

No comments:

Post a Comment