Get the content size returned in the request

Asked

Viewed 39 times

1

Hello, I’m using an Interceptor that will intercept before and after the request. The goal of this is to get the size of the content sent and returned, however, I do not know a way to get the answer, it seems that the sponse has no method to catch 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);
    }
}

1 answer

0

You can read the content-length 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 25 KB size Response can have, uncompressed, more than 120 KB.

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

  • Good afternoon. Face value came null for both (content-length and content-encoding). What was there ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.