POST Attributes with Restlet 2
April 28th, 2011 by nilsSince 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> ...
God thank you. I spent about a day trying everything I could think to try and figure out how to send a post with parameters. It’s not very well documented on the restlet site, but what I was trying to do is working now.
Comment by Devin — May 25, 2011 @ 8:14 pm
Hi,
in my case (restlet jee 2.1m4) my serverResource didn’t get any representation sent by POST until I sent it in the following way (form.getWebRepresentation()) from my client:
Form form = new Form();
etc.
Request request = new Request(method, reference, form.getWebRepresentation());
etc.
Only in this way my
@Post
public Representation acceptItem(Representation entity) {..}
or
@Post
public Representation acceptItem(Form entity) {..}
actually receive a not-null argument.
Comment by myr — June 1, 2011 @ 11:38 am