POST Attributes with Restlet 2

April 28th, 2011 by nils

Since I keep forgetting how to do this, here is a code snippet to POST parameters to some URL using Restlet 2:

Client restletClient = new Client(new Context(), Protocol.HTTP);
Reference resourceRef = new Reference("THE URL...");
resourceRef.addQueryParameter("key", "value");
Request request = new Request(Method.POST, resourceRef);

Response response = restletClient.handle(request);
String responseAsText = response.getEntityAsText();

Oh, and you’ll want to add the following dependencies to your maven pom.xml (if you’re using maven…):

...
<dependency>
  <groupId>org.restlet.jee</groupId>
  <artifactId>org.restlet</artifactId>
</dependency>
<dependency>
  <groupId>org.restlet.jee</groupId>
  <artifactId>org.restlet.ext.json</artifactId>
</dependency>
<dependency>
  <groupId>org.restlet.jee</groupId>
  <artifactId>org.restlet.ext.httpclient</artifactId>
</dependency>
<dependency>
  <groupId>org.restlet.jee</groupId>
  <artifactId>org.restlet.ext.xstream</artifactId>
  <version>2.0.1</version>
</dependency>
<dependency>
  <groupId>org.restlet.jee</groupId>
  <artifactId>org.restlet.ext.xml</artifactId>
</dependency>
...