Tuesday, October 23, 2007

Groovy REST client with Apache httpclient (setting accept request-header value)

Been doing quite a bit with REST style services in Grails. In particular trying to follow closes REST best practices approaches. One of these approaches is to inspect the HTTP Accept header values. (Ref: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html).

While creating these services a person really needs a simple client that they can alter the accept request-header field with. I created one using the Apache httpclient package (http://jakarta.apache.org/httpcomponents/httpclient-3.x/).

Simply download and place the commons-httpclient-*.jar file into your groovy lib directory (or somewhere you class loader will find it) and generate a Groovy program like:


import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.methods.GetMethod;

def url = "URL TO SERVICE"
client = new HttpClient()
get = new GetMethod(url)
get.setRequestHeader("Accept", "text/xml")
client.executeMethod(get)

println get.getResponseBodyAsString().toString()


It's a simple client and one can alter the ACCEPT header value easily to check and validate REST services response to various settings of accept request-header.

5 comments:

Scott Davis said...

HttpClient is a great library to keep around, but you can also do quite a bit with plain old Groovy. For example: "http://www.aboutgroovy.com".toURL().text returns the contents of that page. One liners like this are pretty intoxicating... (grin) Nice post, none-the-less.

zack said...

Actually my Jetty (using Grails 1.0 RC3) throws an error on responding:

[Fatal Error] :-1:-1: Premature end of file

It is non-critical, the response can be delivered to the client. When commenting out the line ...

get.setRequestHeader("Accept", "text/xml")

... all is fine.

Firass said...

Very nice and simple...

Another approach for manipulating HTTP headers is by using LiveHTTPHeaders plugin in FireFox (https://addons.mozilla.org/en-US/firefox/addon/3829), which can come very handy.

Rails junkie said...

Thanks. This was very helpful.
GET works perfect.I am not able to make POST work with this. For some reason,my controller does not see the posted XML content.I get an Null Pointer exception in my controller.I would appreciate any help here. thanks
Here is my code

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.FileRequestEntity;
import org.apache.commons.httpclient.methods.RequestEntity;


def url = "http://localhost:8080/Tagalyzer/form/"
File input = new File("d:/work/tools/curl/form.xml");
client = new HttpClient()
post = new PostMethod(url)
post.setRequestHeader("Accept", "text/xml")
RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
get.setRequestEntity(entity);
client.executeMethod(post)

println get.getResponseBodyAsString().toString()

Tom said...

I developed an HTTP client framework for Groovy which puts a pretty face on Apache HttpClient and makes it feel a bit more like Prototype.js' Ajax.Request. The latest snapshot version also includes a REST client as well.

If you get a chance to try it out, let me know what you think!
http://groovy.codehaus.org/modules/http-builder/