我尝试使用 WebClient (spring-web 5.2.9.RELEASE) 发送 POST 多部分请求,但缺少 API 所需的“Content-Length” header 。
是否可以将 WebClient 配置为包含用于使用 multipart/form-data 媒体类型写入请求的 MultipartHttpMessageWriter 的“Content-Length” header ?
我以这种方式发送请求:
webClient.post().body(BodyInserters.fromMultipartData((MultiValueMap<String, HttpEntity<String>>)request)).exchange();
最佳答案
像这样创建 ExchangeFilterFunction
https://github.com/spring-projects/spring-framework/issues/26489#issuecomment-896182570并注册它,例如
WebClient webClient = WebClient.builder()
.filter(new MultipartExchangeFilterFunction())
...
.build();
其中MultipartExchangeFilterFunction
实现org.springframework.web.reactive.function.client.ExchangeFilterFunction
,计算正文长度并创建Content-Length
header 。
关于java - Content-Length header 使用 WebClient 发送针对 multipart/form-data 的 POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65684250/