Skip to content

ContentCachingResponseWrapper should not add “Content-Length” when “Transfer-Encoding” is present #26182

@KasunAbey

Description

@KasunAbey

Affects: <4.3.21>


I'm using ContentCachingResponseWrapper to read response information in a inherited OncePerRequestFilter.doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) level.
At the end I'm calling ContentCachingResponseWrapper.copyBodyToResponse() to copy response body.

When my API returns ResponseEntity with custom body type, I'm getting following Parse Error;
Parse Error: The response headers can't include "Content-Length" with chunked encoding

This happens when I use Spring Cloud OpenFeign web client to do the API call. So in the OpenFeign GET response includes "transfer-encoding" in the headers and then when I call ContentCachingResponseWrapper.copyBodyToResponse() it is adding "Content-Length" to the headers.

My code is as follows;

OncePerRequestFilter overriden method;

@Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
        throws ServletException, IOException
    {
        ContentCachingRequestWrapper requestWrapper = new ContentCachingRequestWrapper(request);
        ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);

        try
        {
            if (ocpHttpTraceService.isLoggingPropertyEnabled(TracingConstants.BODY, TraceType.INBOUND))
            {
                filterChain.doFilter(requestWrapper, responseWrapper);
            }
            else
            {
                filterChain.doFilter(request, response);
            }

            System.out.println(getRequestBody(requestWrapper));
            System.out.println(getResponseBody(responseWrapper));
        }
        finally
        {
            responseWrapper.copyBodyToResponse();
        }
    }

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)status: backportedAn issue that has been backported to maintenance branchestype: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions