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)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With