Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting "java.lang.IllegalArgumentException: Don't know how to encode XXX as a byte stream."

I want to extract JSON data from a string. Here is my code:

String APIBody = "{\"queryString\": \"Pearson AND unscrubbed:false\"}";
RequestSpecBuilder rbuild = new RequestSpecBuilder();
rbuild.setBody(APIBody);
rbuild.setContentType("application json;charset = UTF-8");
RequestSpecification rSpec = rbuild.build();
Response resp = given().headers(headers).spec(rSpec).when().post("https://content-service.stg-prsn.com/csg/api/v2/search");

In the final line, I am passing multiple headers as a hashmap.

I get the following error:

java.lang.IllegalArgumentException: Don't know how to encode {"queryString": "Pearson AND unscrubbed:false"} 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 json", ContentType.TEXT))). .."
  at com.jayway.restassured.internal.http.EncoderRegistry.encodeStream(EncoderRegistry.java:129)
like image 876
Prasaad C Avatar asked Jun 18 '16 10:06

Prasaad C


1 Answers

I seems that RestAssured is not clever enough to understand the content type JSON with the charset. The easy solution is to replace

setContentType("application json;charset = UTF-8");

With the enum io.restassured.http.ContentType:

setContentType(ContentType.JSON)
like image 102
borjab Avatar answered Sep 18 '22 17:09

borjab