Search code examples
restlet

Restlet: Get response body on failed get()


I am trying to debug a particular connection that responds with a Unauthorized (401). In Restlet this causes Respresentation.get() to throw an error.

What I want to do is get the response body as this particular api gives you greater error information in the body.

Any suggestions?

Thanks, Luke


Solution

  • I would be reluctant to reccomend it for production but certainly for debugging you can override handleInbound in ClientResource to change the conditions under which an error is thrown. Restlet will then return the body, in the usual way.

    ClientResource clientResource = new ClientResource("http://your.url") {
      @Override
      public Representation handleInbound(Response response) {
        Representation result = null;
    
        if (response.getRequest().isSynchronous()) {
          if (response.getStatus().isError() 
            && !Status.CLIENT_ERROR_UNAUTHORIZED.equals(response.getStatus())) {
              doError(response.getStatus());
          } else {
              result = (response == null) ? null : response.getEntity();
          }
        }
    
        return result;
      }
    };
    Representation response = clientResource.get();
    System.out.println(response.getText());