Allow to make FormHttpMessageConverter add the charset in the Content-Type header [SPR-16613] #21154
Labels
in: web
Issues in web modules (web, webmvc, webflux, websocket)
type: enhancement
A general enhancement
Milestone
spring-projects-issues commentedMar 19, 2018
Mauro Molinari opened SPR-16613 and commented
When writing out a form that does not need multipart encoding,
FormHttpMessageConverter
does this inorg.springframework.http.converter.FormHttpMessageConverter.writeForm(MultiValueMap<String, String>, MediaType, HttpOutputMessage)
: if the request headers do not specify a Content-Type (which is the case most of the times, when you useRestTemplate
and theRequestEntity
is built automatically from the supplied HTTP method and body object),application/x-www-form-urlencoded
is added as the content type and the charset used for encoding parameter values is by default (if not specified otherwise) UTF-8.So, the default behaviour of
FormHttpMessageConverter
is to encode parameters with UTF-8 and assume the server does the same (to decode them) when justapplication/x-www-form-urlencoded
is specified as a content type.However, there are cases in which the server may use another "implicit" encoding for this kind of data: Tomcat, for instance, uses ISO-8859-1 in its default configuration (or at least it did so until version 6 or 7, I've not checked what happens in more recent versions). Today I also encountered another web server, running PHP scripts, that assumes ISO-8859-1 if no encoding is specified in the Content-Type.
The solution to this problem is, either:
FormHttpMessageConverter
, like ISO-8859-1 in the above example: this is however cumbersome (it requires to cycle through all the default registeredHttpMessageConverters
on theRestTemplate
to find the correct one and change the encoding) and it also limits the characters that can be correctly encoded and transferred to the server (as an example: ISO-8859-1 is missing the € sign)FormHttpMessageConverter
explicitly set the charset in the Content-Type, like this:Content-Type: application/x-www-form-urlencoded;charset=UTF-8
IMHO, I can't think of a good reason for which
FormHttpMessageConverter
shouldn't do the latter by default (compatibility reasons with very old servers??). In my real-world scenario I see that I can make the server correctly use UTF-8 to decode my form data when I explicitly set the charset in the content type, overriding the default ISO-8859-1 that is used otherwise.Extending
FormHttpMessageConverter
to do this is really a pain, because this class was not designed with extensibility in mind: there's not getter for the privatecharset
field, many methods (likewriteForm(...)
andisMultipart(...)
) are private, no getter forpartConverters
, etc.Affects: 5.0.4
Referenced from: commits 2e4963f, 5861e96, 1897d8e
0 votes, 5 watchers
The text was updated successfully, but these errors were encountered: