Mirko Adebahr opened SPR-16892 and commented
We're trying to use WebClient for creating a non-blocking POST request. The request results in a 400 Bad Request due to a missing content-length header. As the requested endpoint belongs to a 3rd party application, we cannot change that server's behaviour.
In #21085, the EncoderHttpMessageWriter was enhanced to automatically set the content-length Header if the provided publisher is a Mono. Unfortunately, this does not help when using Jackson2JsonEncoder for encoding as getContentLength will always return null.
In my understanding, for MonoJusts and FluxJusts it should be possible to determine the content length and set the header value accordingly.
Affects: 5.0.6
Issue Links: - #21085 EncoderHttpMessageWriter never sets Content-Length - #19562 Content-Length should be set in the request when using reactive WebClient
Referenced from: commits https://github.com/spring-projects/spring-framework/commit/010352163ba4d7f754c019912dc67cb31ef5702a, https://github.com/spring-projects/spring-framework/commit/4a26f93a0dcc9e1839b2bc1e4a6684d085a6f80e, https://github.com/spring-projects/spring-framework/commit/7bfd68381610c145a780744b4507d4c5c0237610, https://github.com/spring-projects/spring-framework/commit/d1c9401dc23a6a2eadb16a171824f25cd08bd98e
Comment From: spring-projects-issues
Rossen Stoyanchev commented
For Jackson we'd have to buffer the serialized content in memory before writing anything out, so the trade-off is between having Content-Length and requiring less memory.
For Mono it would be straight-forward to set Content-Length always regardless of MonoJust or not. In fact looking back at the solution for #21085 I don't even think we need a getContentLength method on Encoder in order to do that.
For Flux we can't do this automatically since it can impact how much memory is used to buffer the serialized content. What we can do is expose a property like bufferForContentLength or something similar.
Comment From: spring-projects-issues
Mirko Adebahr commented
For
Monoit would be straight-forward to set Content-Length always regardless ofMonoJustor not. In fact looking back at the solution for #21085 I don't even think we need agetContentLengthmethod onEncoderin order to do that.
As this would solve our specific issue it would be perfectly sufficient. I just took it for granted that the encoder has to provide the content length thus the issue title.
Still, it is possible that the alternative options you mentioned could be valuable for other users - I myself found it difficult to understand why some Encoder implementations do support getContentLength while others don't.
Comment From: spring-projects-issues
Rossen Stoyanchev commented
Ok so just to be clear, if we ensured Content-Length for Mono, that would solve your needs?
Comment From: spring-projects-issues
Mirko Adebahr commented
Yes, absolutely.
Comment From: privettoli
@rstoyanchev would it be possible to implement similar behavior for MultipartHttpMessageWriter? Similar use-case, breaking compatibility with legacy service when migrating from RestTemplate to WebClient. It seems like the receiving legacy service is running on top of PHP and FastCGI and has this problem https://bugs.php.net/bug.php?id=60826.