springBootVersion = '2.3.4.RELEASE'
I want to download file from server via WebClient and get body with filename, but Content-Disposition header is ignored.
webClient.get()
// some code
.retrieve()
.toEntity(Resource::class.java)
I think that is problem: https://github.com/spring-projects/spring-framework/blob/74f64c4e3b97cb419829cedcc6f703a78f9d0a5c/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponse.java#L93
Next code is ignored: https://github.com/spring-projects/spring-framework/blob/5.2.x/spring-webflux/src/main/java/org/springframework/web/reactive/function/BodyExtractors.java#L210
and: https://github.com/spring-projects/spring-framework/blob/7758ba3c7e4f21435ed927417eb3a13c37af5551/spring-web/src/main/java/org/springframework/http/codec/ResourceHttpMessageReader.java#L51
Comment From: bclozel
Hi @dmkost
Right now the decoder "hints" are essentially used on the server side. You issue description implies that the Optional.empty()
is the problem, but it merely shows that we only support the Content-Disposition
header on the server side, mostly for multipart requests.
We could indeed consider supporting this header on the client side, but we might need to introduce hints on the client side as well, which is not the case at the moment.
For the record, I can reproduce this behavior with the following code snippet:
@SpringBootTest
class ResourceApplicationTests {
@Autowired
WebClient.Builder builder;
@Test
void resourceFilename() {
WebClient client = builder.baseUrl("https://httpbin.org").build();
Resource resource = client.get()
.uri(uriBuilder ->
uriBuilder.path("/response-headers")
.queryParam("Content-Type", "text/plain; charset=UTF-8")
.queryParam("Content-Disposition", "attachment;filename=\"test.json\"")
.build())
.retrieve()
.bodyToMono(Resource.class)
.block();
assertThat(resource.getFilename()).isEqualTo("test.json");
}
}
Comment From: rstoyanchev
This was already done in with #25516. The above snippet runs green with 5.3.