How to get response body in Zuul post filter?

前端 未结 6 1272
时光说笑
时光说笑 2021-01-04 06:55

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:

@Componen         


        
6条回答
  •  南笙
    南笙 (楼主)
    2021-01-04 07:55

    If someone is struggling with compressed answer, here's the solution I used:

    // Read the compressed response
    RequestContext ctx = RequestContext.getCurrentContext();
    InputStream compressedResponseDataStream = ctx.getResponseDataStream();
    try {
        // Uncompress and transform the response
        InputStream responseDataStream = new GZIPInputStream(compressedResponseDataStream);
        String responseAsString = StreamUtils.copyToString(responseDataStream, Charset.forName("UTF-8"));
        // Do want you want with your String response
        ...
        // Replace the response with the modified object
        ctx.setResponseBody(responseAsString);
    } catch (IOException e) {
        logger.warn("Error reading body", e);
    }
    

提交回复
热议问题