1

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();
maximusKon
  • 136
  • 9
  • Check if it answers your question: https://stackoverflow.com/questions/50492890/missing-content-length-header-sending-post-request-with-webclient-springboot-2 – Lucas Campos Jan 12 '21 at 12:59
  • I saw, but it seems, that for MultiValueMap this way to calculate Content-Length doesn't work :( – maximusKon Jan 12 '21 at 13:06

1 Answers1

1

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.

Kyrylo Semenko
  • 866
  • 9
  • 11