question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Content Type being append with charset=ISO-8859-1

See original GitHub issue

Hello!

I’m updating a Spring Boot 2.2 app with Spring Cloud Hoxton.SR1 to Spring Boot 2.3 with Hoxton.SR5, and the provider tests started to fail. The test fail with the following message:

DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=ISO-8859-1' not supported]

Something is appending the charset=ISO-8859-1 to the Content-Type header, even tho our contract DSL sets it to application/json only:

Contract.make {
    description 'create something'
    request {
        method POST()
        url '/something'
        body(
            property: 'value'
        )
        headers {
            contentType(applicationJson()) // This is "application/json"
        }
    }
    response {
        status OK()
    }
}

I think UTF-8 is the default for Spring Boot 2.2 onward, so it was not necessary anymore to use applicationJsonUtf8() as content type. If I do change the content type to applicationJsonUtf8(), then that charset=ISO-8859-1 is not appended and the test pass.

Is appending a default charset an expected behavior for Spring Cloud Contract?

Best regards, Rafael Pacheco.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:14
  • Comments:16 (5 by maintainers)

github_iconTop GitHub Comments

15reactions
LiaminRomancommented, Jun 22, 2020

Hi all, I had a simial issue, here my investigations: Spring Boot 2.3 uses Spring-web 5.2.7, (Spring Boot 2.2 used Spring-Web 5.2.6) Spring-web v.5.2.7 has a class AbstractJackson2HttpMessageConverter and it was modified - it started to check encoding for Json body (canRead(@Nullable MediaType mediaType)), so now only null encodign or UTF-xx supported.

On other hand RestAssured (3.3 in my case) by default uses ISO_8859_1 as defaultContentCharset (see deafult constructor of RestAssuredMockMvcConfig -> new EncoderConfig()) and if you don’t specify any charset excplisitly in the request content-type - it will add the defalt one.

So Spring Boot 2.3 (Spring Web 5.2.7) becomes more strict to headers.

My solution was to bring custom configuration to RestAssured: RestAssuredMockMvc.config = new RestAssuredMockMvcConfig() .encoderConfig(new EncoderConfig(UTF_8.name(), UTF_8.name()));

or opt-out appending of default content charset: EncoderConfig encoderConfig = new EncoderConfig() .appendDefaultContentCharsetToContentTypeIfUndefined(false); RestAssuredMockMvcConfig restAssuredConf = new RestAssuredMockMvcConfig() .encoderConfig(encoderConfig); RestAssuredMockMvc.config = restAssuredConf;

5reactions
marcingrzejszczakcommented, Jul 24, 2020

Indeed until Boot bumps Rest Assured to 4.x I guess we need to do


		EncoderConfig encoderConfig = new EncoderConfig().appendDefaultContentCharsetToContentTypeIfUndefined(false);
		RestAssuredMockMvc.config = new RestAssuredMockMvcConfig().encoderConfig(encoderConfig);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Content-type header in response is getting appended by ...
Internally, it would append charset=ISO-8859-1 to the content-type header if not specified. As I wanted to remove this, changed method ...
Read more >
Why does the browser add or replace the Content-type ...
Then append ` ;charset=UTF-8 ` to Content-Type. String: ... UTF-8 & ISO-8859-1 are identical to ASCII for the values from 0 to 127....
Read more >
Character Encoding Issues - Apache Software Foundation
I'm having a problem with character encoding in Tomcat 5 ... An example of such a header is Content-Type: text/html; charset=ISO-8859-1 which explicitly ......
Read more >
HTTP authentication does not support non-ISO-8859-1 ...
(maybe any charset given in the Content-Type header of the response ... If (a), we should at least stop non-ISO-8859-1 characters from being...
Read more >
Character sets/Encoding - IBM
The default character encoding when reading is iso-8859–1. ... overridden in turn by a header of the type " Content-type: text/plain; charset=iso-8859–1 "....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found