API 自动化:在 "GET" 请求上接收类似“`io.restassured.path.json.JsonPath@12345xxx”的输出
API Automation: Receiving Output like "`io.restassured.path.json.JsonPath@12345xxx " on "GET" request
我是 REST assured 和 API 自动化的新手,我使用“GET”请求尝试了以下代码:
public static void main(String args[]) throws IOException {
RestAssured.baseURI = "{Url}";
RequestSpecification httpRequest = RestAssured.given()
.contentType("x-www-form-urlencoded")
.formParam("grant_type", "")
.formParam("client_id", "")
.formParam("scope", "")
.formParam("client_secret","");
Response response = httpRequest.request(Method.GET);
JsonPath res = response.jsonPath();
//String BearerToken = response.getBody().asString();
System.out.println("The response is:" +res);
int code = response.getStatusCode();
System.out.println("The status code is" +code);
assertEquals(code, 200);
}
}
我得到以下输出
io.restassured.path.json.JsonPath@12345xxx
这不对,因为我需要收到适当的 JSON 作为回复。
请注意:我隐藏了一些与安全风险相关的值。
有人可以帮我解决这个问题吗?
要打印来自 Rest Assured 的响应,您可以在 Response
对象上使用 asString()
方法。您不需要使用 JsonPath 来打印结果:
Response response = httpRequest.request(Method.GET);
System.out.println("The response is:" + response.asString());
或者您可以使用 JsonPath 的 prettyPrint()
方法:
Response response = httpRequest.request(Method.GET);
JsonPath res = response.jsonPath();
res.prettyPrint(); //no need to call System.out.println
我是 REST assured 和 API 自动化的新手,我使用“GET”请求尝试了以下代码:
public static void main(String args[]) throws IOException {
RestAssured.baseURI = "{Url}";
RequestSpecification httpRequest = RestAssured.given()
.contentType("x-www-form-urlencoded")
.formParam("grant_type", "")
.formParam("client_id", "")
.formParam("scope", "")
.formParam("client_secret","");
Response response = httpRequest.request(Method.GET);
JsonPath res = response.jsonPath();
//String BearerToken = response.getBody().asString();
System.out.println("The response is:" +res);
int code = response.getStatusCode();
System.out.println("The status code is" +code);
assertEquals(code, 200);
}
}
我得到以下输出
io.restassured.path.json.JsonPath@12345xxx
这不对,因为我需要收到适当的 JSON 作为回复。 请注意:我隐藏了一些与安全风险相关的值。
有人可以帮我解决这个问题吗?
要打印来自 Rest Assured 的响应,您可以在 Response
对象上使用 asString()
方法。您不需要使用 JsonPath 来打印结果:
Response response = httpRequest.request(Method.GET);
System.out.println("The response is:" + response.asString());
或者您可以使用 JsonPath 的 prettyPrint()
方法:
Response response = httpRequest.request(Method.GET);
JsonPath res = response.jsonPath();
res.prettyPrint(); //no need to call System.out.println