Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Content-Length header sending POST request with WebClient for multipart/form-data

I'm trying to send POST multipart request using WebClient (spring-web 5.2.9.RELEASE), but it is missing "Content-Length" header required by the API.

Is it possible to configure WebClient to include "Content-Length" header for MultipartHttpMessageWriter which using to write request with multipart/form-data media type?

I'm sending request in this way:

webClient.post().body(BodyInserters.fromMultipartData((MultiValueMap<String, HttpEntity<String>>)request)).exchange();
like image 861
maximusKon Avatar asked Jun 19 '26 21:06

maximusKon


1 Answers

Create ExchangeFilterFunction like this one https://github.com/spring-projects/spring-framework/issues/26489#issuecomment-896182570 and register it, for example

WebClient webClient = WebClient.builder()
                .filter(new MultipartExchangeFilterFunction())
                ...
                .build();

Where MultipartExchangeFilterFunction implements org.springframework.web.reactive.function.client.ExchangeFilterFunction, calculates body length and creates the Content-Length header.

like image 108
Kyrylo Semenko Avatar answered Jun 22 '26 12:06

Kyrylo Semenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!