Search code examples
javarestlet

Restlet Filter Post request


I would like to filter a Post request in Filter (prior it getting to the Resource). To filter the request I want to retrieve a token from the bode request and do some testing on it.

Current Resource:

@Post
public JsonRepresentation init(JsonRepresentation jRep) {
    String token = jRep.getJsonObject().getString("token");
    .
    .
    .
}

Current Filter:

@Override
protected int beforeHandle(Request request, Response response) {
  int result = STOP;
  String token = (String) Request.getCurrent().getAttributes().get("token");
  .
  .
  .
}

These code does not retrieve the token.

My question is how can I retrieve a body request?


Solution

  • You can directly get the payload text of the request from its associated entity object, as described below:

    Representation repr = request.getEntity();
    String content = repr.getText();
    

    Hope it helps you, Thierry