Monday, October 8, 2007

RESTlet, POST and the HTTP Location Header

I spent a few hours this last weekend trying to get POST working in a RESTlet application I am developing for work. The problem is that RESTlet abstracts the fact you are using HTTP completely. So in my code I could not grab a HTTP response object and set headers directly. Not a big deal as long as the process of POST'ing in RESTlet is documented. It is not though... I'm reading up on REST and trying to build a well done implementation. That means using the HTTP location header to return a new resources location on POST. I dragged my nets across all documentation RESTlet and could not figure this out. I new it must be doable so I just started pushing the api methods and such, trying to find something. Finally I figured out what is needed. Here is a code snippet:

* POST a document to resource. Should create a new resource with a new and
* unique UUID.
public void post(Representation representation)

So what you see above is the simple way to create a new resource via POST. The 'insertNewResource' method would be your code which creates the resource in your underlying application. Once that is done you must use the 'redirectSeeOther' method to set the HTTP location header. You also will want to use 'setStatus' to properly notify the client that the resource was created.


Adam said...

Thank you very much, this saved me quite a few hours of effort! :)

Jerome Louvel said...

Hi Robert,

In Restlet 1.1 M1, there is now a Response.setLocationRef() method that you can use. In version 1.0 it was named setRedirectRef() which proved to be unintuitive :)

Best regards,

robottaway said...

Hello J,

Been playing around with the 1.1 early stuff. I like some of the new features. A number of folks at work, including myself, have put RESTlet to use multiple times in working applications. It is simple and well thought out, like REST itself.

I've not really made it crash proof yet but a colleague and myself have extended RESTlet to allow easy Atom Publishing Protocol implementations. Just added a small number of extension classes (less than ten I believe). Take a look at the early documentation:

I love how easy RESTlet can be molded to fit REST extension protocols such as APP. Keep up the good work fellow!