1
votes

I am trying to test error message returned by our API when invalid encoding is specified in the request. I am sending POST request with plain text body and Content-Type header "application/html". I am able to do that successfully from Postman. However RestAssured prompts to specify encoding directly.

Don't know how to encode  {...} as a byte stream.
Please use EncoderConfig (EncoderConfig#encodeContentTypeAs) to specify how to serialize data for this content-type.
For example: "given().config(RestAssured.config().encoderConfig(encoderConfig().encodeContentTypeAs("application/html", ContentType.TEXT))). .."

But even when I do that directly I get the same error from RestAssured:

RestAssured.given().config(RestAssured.config().encoderConfig(EncoderConfig.encoderConfig().encodeContentTypeAs("application/html", ContentType.HTML)));

Here are the headers I use:

RestAssured.given().header("Content-Type", "application/html")

.header("accept", "application/json");
1

1 Answers

1
votes

You can set response type of the request and also verify the content type of the response using the below code .

given().contentType("application/json; charset=utf-8").when().get("http://localhost:3000/posts/").then().statusCode(200)
        .and().contentType("application/json; charset=utf-8");