Sunday, December 13, 2009

Get Request Header in Restlet

When using Restlet library to invoke RESTful method, sometimes you need to parse the request header for further use. And luckily, though there is explicit ways to solve this problem, we can still get what we need.

Calling getRequestAttributes().get("org.restlet.http.headers"); will return all the headers in the request. However, the type of returned result is "org.restlet.data.Form". Consequently, we need to convert it into the Form format, and then using the getFirstValue("") method to retrieve the value. One catch there is the key is always start with capital letter, for instant even is "location" in the header, you have to use "Location" instead.

Form headers = (Form) getRequestAttributes().get("org.restlet.http.headers");
location = headers.getFirstValue("Location");

4 comments:

  1. It returns Series instead of Form

    ReplyDelete
  2. org.restlet.util.Series cannot be cast to org.restlet.data.Form

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Series restletHeaders = (Series)getRequestAttributes().get("org.restlet.http.headers");
    location = headers.getFirstValue("Location");

    ReplyDelete