Play 2.5: get response body in custom http action

I'm trying to create a custom http action ( to log request and response bodies with Play 2.5.0 Java. This is what I've got so far:

public class Log extends play.mvc.Action.Simple {

    public CompletionStage<Result> call(Http.Context ctx) {
        CompletionStage<Result> response =;
        //request body is fine
        //how to get response body string here while also not sabotaging http response flow of the framework?
        //my guess is it should be somehow possible to access it below?
        response.thenApply( r -> {
            return null;
        return response;



Logging is often considered a cross-cutting feature. In such cases the preferred way to do this in Play is to use Filters:

The filter API is intended for cross cutting concerns that are applied indiscriminately to all routes. For example, here are some common use cases for filters:

  • Logging/metrics collection
  • GZIP encoding
  • Security headers

This works for me:

import java.util.concurrent.CompletionStage;
import java.util.function.Function;
import javax.inject.Inject;
import play.Logger;
import play.mvc.*;

public class LoggingFilter extends Filter {

    Materializer mat;

    public LoggingFilter(Materializer mat) {
        this.mat = mat;

    public CompletionStage<Result> apply(
            Function<Http.RequestHeader, CompletionStage<Result>> nextFilter,
            Http.RequestHeader requestHeader) {
        long startTime = System.currentTimeMillis();
        return nextFilter.apply(requestHeader).thenApply(result -> {
            long endTime = System.currentTimeMillis();
            long requestTime = endTime - startTime;

  "{} {} took {}ms and returned {}",
                requestHeader.method(), requestHeader.uri(), requestTime, result.status());
            akka.util.ByteString body = play.core.j.JavaResultExtractor.getBody(result, 10000l, mat);

            return result.withHeader("Request-Time", "" + requestTime);

What is it doing?

First this creates a new Filter which can be used along with other filters you may have. In order to get the body of the response we actually use the nextFilter - once we have the response we can then get the body.

As of Play 2.5 Akka Streams are the weapon of choice. This means that once you use the JavaResultExtractor, you will get a ByteString, which you then have to decode in order to get the real string underneath.

Please keep in mind that there should be no problem in copying this logic in the Action that you are creating. I just chose the option with Filter because of the reason stated at the top of my post.

Need Your Help

Comparing modules available in different Strawberry Perl versions?

perl perl-module cpan strawberry-perl

Background: We have recently got into some confusion, because one developer used Strawberry Perl while the our buildserver uses 5.14.2. However, contains more modules, so the script