spring-cloud/spring-cloud-netflix

Not able to add body to the incoming request

Closed this issue · 0 comments

I am trying to modify the request body of the incoming request.
This is working fine when the incoming request already has a body in it, but when the request does not have a body, i want to manually this body in zuul filter. This use case is not working.

This is the piece of code that i am using to modify the request. consider that modifiedBody will never be null, as this is hardcoded.

private static com.netflix.zuul.http.HttpServletRequestWrapper modifyRequest(HttpServletRequest request, String modifiedBody) {

    return new HttpServletRequestWrapper(request) {
        @Override
        public byte[] getContentData() {
            byte[] data = null;
            try {
                data = body.getBytes(StandardCharsets.UTF_8);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return data;
        }

        @Override
        public int getContentLength() {
            return body.getBytes().length;
        }

        @Override
        public long getContentLengthLong() {
            return body.getBytes().length;
        }

        @Override
        public BufferedReader getReader() {
            return new BufferedReader(new InputStreamReader(new ByteArrayInputStream(body.getBytes(StandardCharsets.UTF_8))));
        }

        @Override
        public ServletInputStream getInputStream() {
            return new ServletInputStreamWrapper(body.getBytes(StandardCharsets.UTF_8));
        }
    };
}

//This is the caller
context.setRequest(modifyRequest(context.getRequest(), modifiedRequest.toString()));

Can anyone provide any suggestion on this?