Skip to content

Commit

Permalink
Expose method to determine form content type
Browse files Browse the repository at this point in the history
This commit exposes the method that returns the media type used to write
forms. By default, it includes the charset in the content type, which
can cause issues with certain consumers. This commit changes the method
from a private to a protected method, so that users can override the
default behavior.

Closes: gh-22971
  • Loading branch information
poutsma committed Nov 26, 2019
1 parent 2d86f22 commit ddb38ee
Showing 1 changed file with 18 additions and 6 deletions.
Expand Up @@ -384,7 +384,7 @@ private boolean isMultipart(MultiValueMap<String, ?> map, @Nullable MediaType co
private void writeForm(MultiValueMap<String, Object> formData, @Nullable MediaType contentType,
HttpOutputMessage outputMessage) throws IOException {

contentType = getMediaType(contentType);
contentType = getFormContentType(contentType);
outputMessage.getHeaders().setContentType(contentType);

Charset charset = contentType.getCharset();
Expand All @@ -402,15 +402,27 @@ private void writeForm(MultiValueMap<String, Object> formData, @Nullable MediaTy
}
}

private MediaType getMediaType(@Nullable MediaType mediaType) {
if (mediaType == null) {
/**
* Return the content type used to write forms, given the preferred content type.
* By default, this method returns the given content type, but adds the
* {@linkplain #setCharset(Charset) charset} if it does not have one.
* If {@code contentType} is {@code null},
* {@code application/x-www-form-urlencoded; charset=UTF-8} is returned.
*
* <p>Subclasses can override this method to change this behavior.
* @param contentType the preferred content type, can be {@code null}
* @return the content type to be used
* @since 5.2.2
*/
protected MediaType getFormContentType(@Nullable MediaType contentType) {
if (contentType == null) {
return DEFAULT_FORM_DATA_MEDIA_TYPE;
}
else if (mediaType.getCharset() == null) {
return new MediaType(mediaType, this.charset);
else if (contentType.getCharset() == null) {
return new MediaType(contentType, this.charset);
}
else {
return mediaType;
return contentType;
}
}

Expand Down

0 comments on commit ddb38ee

Please sign in to comment.