我想从字符串中提取JSON数据。这是我的代码:
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");
在最后一行中,我将多个标头作为哈希表传递。
我收到以下错误:
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)
最佳答案
尝试使用javajson.jar
代替restassured
。我尝试使用下面给出的restassured
api:
Response resp = given().config(RestAssured.config().encoderConfig(((Object) encoderConfig()).encodeContentTypeAs("application json", APIBody)));
但是缺少以下方法/在jar文件中未定义
配置:
对于类型RestAssured,未定义方法config()
encodeContentTypeAs
未定义方法encodeContentTypeAs(String,String)
类型对象