I'm trying to implement streaming proxy. I faced with the issue with WebClient from spring reactive.
Could anyone help me to understand Do I some wrong way or it's just a bug from WebClient side?
Stack:
reactor-netty 0.7.8.RELEASE
spring-boot 2.0.4.RELEASE
Desc:
I want to proxy a long stream to external service and then forward stream of responses to the requester. Streaming is happening using chunks (HTTP 1.1 Transfer-Encoding : chunked). External service processes every chunk and sends to the response result.
Expected behaviour:
WebClient should read every received part of response immediately.
Actual behaviour:
WebClient doesn't start process response until request write is completed.
Code:
return client
.post()
.header("Transfer-Encoding", "chunked")
//because I want to flush each received part
.body((outputMessage, context) -> outputMessage.writeAndFlushWith(
request.body(BodyExtractors.toDataBuffers())
.map(dataBuffer -> Mono.just(dataBuffer))))
.exchange()
.flatMap(clientResponse -> {
ServerResponse.BodyBuilder bodyBuilder = ServerResponse.status(clientResponse.statusCode());
bodyBuilder.contentType(MediaType.APPLICATION_STREAM_JSON);
return bodyBuilder.body((outputMessage, context) ->
outputMessage.writeAndFlushWith(
clientResponse.body(BodyExtractors.toDataBuffers())
.map(dataBuffer -> Mono.just(dataBuffer))
));}
);