5

Rest assured version: 3.0.5

As a user we have passed contentType as XML in below format with valid content.

contentType(ContentType.XML) OR .contentType("application/xml")

In application allowed content type was: "application/xml" Supplied content type was reflected as below.

Content-Type=application/xml; charset=ISO-8859-1

Due to this it was giving error "Content Type is not valid" How to handle this use case.

Diptman
  • 374
  • 3
  • 14

4 Answers4

7

As expected content Type is application/xml but provided content type object includes "charset=ISO-8859-1". Hence we need to remove this charset details.

EncoderConfig encoderconfig = new EncoderConfig();
    Response response = given()
            .config(RestAssured.config()
                    .encoderConfig(encoderConfig().appendDefaultContentCharsetToContentTypeIfUndefined(false)))
            .contentType(ContentType.XML)
            .log().all().body(this.buildPayload()).when().
    post(...).

...;

For more details please refer below links:

https://github.com/rest-assured/rest-assured/wiki/Usage#avoid-adding-the-charset-to-content-type-header-automatically

https://groups.google.com/forum/#!topic/rest-assured/O74EgJWUSJY

Vadzim
  • 24,954
  • 11
  • 143
  • 151
Diptman
  • 374
  • 3
  • 14
2
EncoderConfig EC= new EncoderConfig();

given().config(RestAssured.config()
        .encoderConfig(EC.appendDefaultContentCharsetToContentTypeIfUndefined(false)))
.contentType(ContentType.XML);
ANIL KUMAR
  • 139
  • 1
  • 3
1

If you are using RequestSpecBuilder to build RequestSpecification then you need to remove the charset=ISO-8859-1 before creating the final Specification

private static RequestSpecification specification;

 
RequestSpecBuilder builder = new RequestSpecBuilder();
         builder.setConfig(RestAssuredConfig.newConfig().encoderConfig(EncoderConfig.encoderConfig().appendDefaultContentCharsetToContentTypeIfUndefined(false)));
builder.setContentType(ContentType.XML);
specification = builder.build();
            
// And then you can use in your code like
             
RestAssured.given().spec(specification).body(XMLrequestBody).when().
      post(...).
0

Try below set of code....

given().
        config(RestAssured.config().encoderConfig(encoderConfig().appendDefaultContentCharsetToContentTypeIfUndefined(false))).
        contentType("application/json").
        body(...).
when().
        post(...).
...
Tom Zych
  • 13,329
  • 9
  • 36
  • 53