Get the content size returned in the request

1

Hello, I'm using an interceptor that will intercept before and after the request. The purpose of this is to get the size of the content sent and returned, however, I do not know how to get the answer, it seems that the response does not have a method to get it

    @Component
public class DataUsageInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(
            HttpServletRequest request,
            HttpServletResponse response,
            Object handler
    ) throws Exception {
        System.out.println("Tamanho da requisicao: " + request.getContentLength());
        return super.preHandle(request, response, handler);
    }

    @Override
    public void afterCompletion(
            HttpServletRequest request,
            HttpServletResponse response,
            Object handler, Exception ex
    ) throws Exception {

        System.out.println("Tamanho da resposta: ??");

        super.afterCompletion(request, response, handler, ex);
    }
}
    
asked by anonymous 29.01.2018 / 13:32

1 answer

0

You can read content-length of Response Header to get this information:

response.getHeader("content-length");

This information corresponds to the size of the compressed response (if applicable) by means of Gzip ¹. Then a response of size 25 KB may have, uncompressed, more than 120 KB.

¹: You can check if gzip is enabled by reading the header content-encoding .

    
29.01.2018 / 13:56