Skip to content

Explicit Content-Type Header in WebClient is overwritten by ResourceHttpMessageWriter #24366

@hgarus

Description

@hgarus

I discovered this on spring-boot 2.1 I can reproduce the same behaviour using spring-boot 2.2.2.

I'm trying to post binary Content with a specified Content-Type Header using WebClient:

 WebClient.create().post().uri(MY_URI)
                .contentType(MediaType.APPLICATION_PDF)
                .body(BodyInserters.fromResource(myResource))
                .exchange()

in some cases the request was made with Content-Type: application/octet-stream instead of application/pdf.
Apparently the RessourceHttpMessageWriter will set the Content-Type Header based on the resources filename overwriting the explicitly set contentType on the Request in ResourceHttpMessageWriter.writeResource.

See https://github.com/hgarus/webclient-content-type-example/blob/master/src/test/java/com/example/demo/WebClientContentTypeTest.java for a minimal testcase.

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: bugA general bug

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions