How to get response body in Zuul post filter?

前端 未结 6 1221
时光说笑
时光说笑 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:41

    I've managed to overcome this. The solution consists of 4 steps:

    1. Read ctx.getResponseDataStream() into a ByteArrayOutputStream
    2. Copy OutputStream to 2 InputStreams.
    3. Use one of it for your custom purposes.
    4. Use the second to reassign to context: context.setResponseBody(inputStream)
      • reading stream from point 1 would cause that the stream cannot be read again, so this way you're passing a new fresh stream that wasn't read yet

提交回复
热议问题