How do I implement Restlet function which accepts JSON post? And how do I test this using curl?
Thanks
How do I implement Restlet function which accepts JSON post? And how do I test this using curl?
Thanks
With Restlet 2, you can either:
test the entity media-type compatibility in @Post acceptRepresentation(Representation entity)
:
@Post public Representation acceptRepresentation(Representation entity) throws ResourceException { if (entity.getMediaType().isCompatible(MediaType.APPLICATION_JSON)) { // ... } // ... }
or use @Post
with one or two parameters:
@Post("json") Representation acceptAndReturnJson(Representation entity) { // ... }
See these links:
(With Restlet 1, you would need to test the type of the entity.)
As of writing this response (2 years after your question), Restlet 2.1 requires proper dependencies fulfilled to properly consume and respond with JSON. Point is, apart from "Unsupported media type
" response, there is not much clue about what is going on internally.
To activate JSON media type, you need to include dependency to org.restlet.ext.jackson
; if you need to support both XML and JSON, you need to include Jackson FIRST and then org.restlet.ext.xstream
, as XStream is also capable of JSON representations but the implementation is rather poor (as described in restlet docs, this is recommended order by restlet authors).
Then, you don't actually need to include media type in annotation and you just need to include proper Content-Type
header in your curl request, i.e.:
curl -X post -H "Content-Type: application/json" http://localhost:8080/login -d @login.json
login.json
contains the actual JSON request.@Post
annotated method accepting LoginRequest
and responding with LoginResponse
, both capable of XML and JSON media typesI hope, this answer will help someone sometime. :-)
The example linked to by Daniel Vassallo shows data posted using a form. This is how to send JSON:
@Post public void acceptJsonRepresentation(JsonRepresentation entity) { JSONObject json = null; try { json = entity.getJsonObject(); // business logic and persistence } catch (JSONException e) { setStatus(Status.CLIENT_ERROR_BAD_REQUEST); return; } }
To test with curl:
curl -X POST -H "Content-Type: application/json" -d '{"key" : "value"}'
The single quotes ('') around the data in the curl command are important.
Here are some updates regarding this old question. Restlet supports method signatures that contains beans. In such cases, Restlet will use a registered converter to try to convert / fill the received payload into a bean instance. This is also true when sending content to the client.
Here is the sample of a method that handles a request POST
:
public class TestServerResource extends ServerResource { @Post public void test(TestBean bean) { System.out.println(">> bean = " + bean.getMessage()); } }
The bean can simply have the following structure:
public class TestBean { private String name; private String message; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
To make work such mechanism, you can simply add the extension Jackson (org.restlet.ext.jackson
) within your classpath. The corresponding converter will be automatically registered under the hood.
The curl request is simple and the data to send has to be specified
curl -X POST http://... -H "Content-Type: application/json" -d '{"name" : "myname","description":"my description"}'
Hope it helps you, Thierry
Here is a good and complete example of a Restlet that accepts JSON via POST:
And a basic guide on how to test RESTful web services with cURL:
curl -u uid:4c521655 -X POST -H "Content-Type: application/json" -d "type=Big&data="{\"name\":\"test\"}"" --dump-header headers 'http://localhost:8190/appli/add'