Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide control over fallback charset to use in WebClientResponseException #26866

Closed
j3r0lin opened this issue Apr 27, 2021 · 2 comments
Closed
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Milestone

Comments

@j3r0lin
Copy link

j3r0lin commented Apr 27, 2021

Affects: 5.3.6


Web client should throw an WebClientResponseException when http response status is 4xx, which is created by clientResponse.createException(), if the error response is JSON string, the media type should be application/json, that should decode http response body using UTF-8 encoding by default, but current using ISO-8859-1 for all media types. it causes body field in the generated exception contains malformed characters.

HttpRequest request = this.requestSupplier.get();
Charset charset = headers().contentType()
.map(MimeType::getCharset)
.orElse(StandardCharsets.ISO_8859_1);
int statusCode = rawStatusCode();

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Apr 27, 2021
@rstoyanchev rstoyanchev self-assigned this Apr 29, 2021
@rstoyanchev
Copy link
Contributor

rstoyanchev commented Apr 29, 2021

Indeed, assuming ISO_8859_1 as a default fallback is unfortunate.

@rstoyanchev rstoyanchev added in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Apr 29, 2021
@rstoyanchev rstoyanchev added this to the 5.3.7 milestone Apr 29, 2021
@rstoyanchev
Copy link
Contributor

I made change where you can pass the fallback Charset to use into the getResponseBodyAsString method of WebClientResponseException. I'm afraid that's as far as we can go in a 5.3.7 release. Perhaps in a future major or minor version we can change the default. For extra context, indeed a JSON response should be UTF-8 but following a similar change in other areas, we had plenty of feedback about JSON responses that aren't UTF-8 beyond a client's control.

@rstoyanchev rstoyanchev changed the title Wrong charset used when creating exception from WebClient DefaltClientResponse Provide control over fallack charset to use in WebClientResponseException May 6, 2021
@jhoeller jhoeller changed the title Provide control over fallack charset to use in WebClientResponseException Provide control over fallback charset to use in WebClientResponseException May 11, 2021
Zoran0104 pushed a commit to Zoran0104/spring-framework that referenced this issue Aug 20, 2021
lxbzmy pushed a commit to lxbzmy/spring-framework that referenced this issue Mar 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants