Restlet POST using JSON
Asked Answered
C

6

12

How do I implement Restlet function which accepts JSON post? And how do I test this using curl?

Thanks

Clinton answered 9/1, 2010 at 11:8 Comment(0)
S
10

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.)

Streak answered 24/6, 2010 at 23:12 Comment(2)
Sorry for barging in, but I cannot find a simple way of converting the JSONRepresentation into a POJO.Brainwork
The 2nd link is broken.Lubow
S
8

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
  • where login.json contains the actual JSON request.
  • login is @Post annotated method accepting LoginRequest and responding with LoginResponse, both capable of XML and JSON media types

I hope, this answer will help someone sometime. :-)

Saccharide answered 31/7, 2012 at 11:42 Comment(0)
U
6

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.

Umbilical answered 24/1, 2011 at 18:5 Comment(1)
Sorry for barging in, but I cannot find a simple way of converting the JSONRepresentation into a POJO.Brainwork
J
4

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

Juvenescence answered 22/4, 2015 at 12:37 Comment(1)
Hi Thierry, perhaps you can shed some light on this related problem: #66794541Gregarine
W
2

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:

Watch answered 9/1, 2010 at 11:11 Comment(3)
in the example, the data is still POSTed using standard form-url-encode "name=value". So how can I POST a JSON formatted string { "name": "value" } ?Clinton
The first link is very old. A lot of stuff are manually made, where they could have been done automatically. Be warnedBrainwork
The first link is dead.Chem
A
0
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'
Auston answered 22/5, 2014 at 11:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.