When I send a multipart request using ApiPost to my Spring WebFlux endpoint, I'm getting an error indicating "Content type 'application/octet-stream' not supported".
Here's the relevant part of my controller:
@PostMapping(value = "upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public Mono<String> uploadFile(@RequestPart("metadata") FileDto fileDto,@RequestPart("files") Flux<Part> files) throws IOException {
long start = System.currentTimeMillis();
MultiValueMap<String, Object> formData = new LinkedMultiValueMap<>();
Mono<String> mono = files.flatMap(file -> {
formData.add("files", file);
return Mono.empty();
})
.thenMany(
webClient.post()
.uri("http://localhost:8081/upload")
.contentType(MediaType.MULTIPART_FORM_DATA)
.body(BodyInserters.fromMultipartData(formData))
.retrieve()
.bodyToMono(String.class)
)
.last()
.defaultIfEmpty("Files forwarded successfully.");
System.out.println(System.currentTimeMillis()-start);
return mono;
}
error: 2023-10-24 14:49:05.525 WARN 27364 --- [nio-8082-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/octet-stream' not supported]
I expect the endpoint to correctly receive the multipart request, process it, and forward the files to another service.
Comment From: livk-cloud
When I send a multipart request using ApiPost to my Spring WebFlux endpoint, I'm getting an error indicating "Content type 'application/octet-stream' not supported".
Here's the relevant part of my controller:
java @PostMapping(value = "upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public Mono<String> uploadFile(@RequestPart("metadata") FileDto fileDto,@RequestPart("files") Flux<Part> files) throws IOException { long start = System.currentTimeMillis(); MultiValueMap<String, Object> formData = new LinkedMultiValueMap<>(); Mono<String> mono = files.flatMap(file -> { formData.add("files", file); return Mono.empty(); }) .thenMany( webClient.post() .uri("http://localhost:8081/upload") .contentType(MediaType.MULTIPART_FORM_DATA) .body(BodyInserters.fromMultipartData(formData)) .retrieve() .bodyToMono(String.class) ) .last() .defaultIfEmpty("Files forwarded successfully."); System.out.println(System.currentTimeMillis()-start); return mono; }
error: 2023-10-24 14:49:05.525 WARN 27364 --- [nio-8082-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/octet-stream' not supported]
I expect the endpoint to correctly receive the multipart request, process it, and forward the files to another service.
"metadata" uses @RequestPart, but the transmitted data is of type TEXT, As far as I know, such data should be used @RequestParam
Comment From: sdeleuze
Please provide a reproducer, either as an attached Maven/Gradle project or by sharing such project via a Git repository.
Comment From: wjzxc123
Please provide a reproducer, either as an attached Maven/Gradle project or by sharing such project via a Git repository.
you can use test.htttp
https://github.com/wjzxc123/web-flux-demo
Comment From: sdeleuze
As far as I can tell, WebFlux server is not used in the module A of the repro because both Spring MVC and Spring WebFlux are in the classpath, if you only put WebFlux in module A classpath, I guess @RequestPart("files") Flux<FilePart> files
handling will work as expected (not tried).