tags:

views:

855

answers:

2

Hi,

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

Thanks

+3  A: 

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:

Daniel Vassallo
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" } ?
portoalet
+2  A: 

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

Bruno