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 <your url> -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
I m using Java to parse this request http://ajax.googleapis.com/ajax/services/search/web?start=0&rsz=large&v=1.0&q=rz+img+news+recordid+border which has as a result this (truncated for ...
I know this has been an issue for others, but I ve yet to find anything that fixes my problem. I have a partial view that is displayed in a lightbox (colorbox). It is a simple form. I want the form ...
I have inherited a piece of work where the entry screen shows a summary of 20 calculated variables. E.g. Var A (250), Var B (79). Clicking on any of these links takes the user to a view with a ...
I ve implemented the quicksearch plugin by Rik Lomas and I love it for an application in a custom CMS I m building. I was wondering though, since I m going to have a bizillion items in the table if ...
Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...
i m trying to use json_decode to combine a few json objects and then re-encode it. my json looks like: { "core": { "segment": [ { "id": 7, "...
I can easily do this with JQuery or PHP but I have a project for my Intro to C++ class and I thought it ll be pretty cool if I could mix C++ with some APIs like twitter, google, yahoo etc. Could you ...
I want to be able to access properties from a JSON string within my Java action method. The string is available by simply saying myJsonString = object.getJson(). Below is an example of what the string ...