How it is possible to read a response body while using Zuul as a proxy in post
filter?
I am trying to call the code like this:
@Component
public class PostFilter extends ZuulFilter {
private static final Logger log = LoggerFactory.getLogger(PostFilter.class);
@Override
public String filterType() {
return "post";
}
@Override
public int filterOrder() {
return 2000;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
ctx.getResponseBody(); // null
// cant't do this, cause input stream is used later in other filters and I got InputStream Closed exception
// GZIPInputStream gzipInputStream = new GZIPInputStream(stream);
return null;
}
}
Be careful the filterNumber
I used the number 10 and worked fine
As you can see in this example, you have two methods available to extract the response body:
1- ctx.getResponseBody();
2- ctx.getResponseDataStream();
You have to check which one is not null and use that one.
None of the answers worked for me. 1) Order of the filter needs to be lower that 1000 (sending response filter)
2) Code:
Thanks for suggestion, this is the code I used that works.
I've managed to overcome this. The solution consists of 4 steps:
ctx.getResponseDataStream()
into a ByteArrayOutputStreamcontext.setResponseBody(inputStream)
If someone is struggling with compressed answer, here's the solution I used: