如何在 Rest Assured 中将整个 JSON 响应作为字符串提取出来?
我们可以在 Rest Assured 中将整个 JSON 提取为一个字符串。这是借助 extract 方法实现的。它将使用 asString 方法将整个响应提取为一个字符串。
我们将通过 Postman 在一个模拟 API 上发送一个 GET 请求,观察响应。

示例
使用 Rest Assured,我们可以以字符串格式获取整个响应。
代码实现
import org.testng.annotations.Test;
import static io.restassured.RestAssured.given;
import io.restassured.RestAssured;
public class NewTest {
@Test
public void getResponseAsString() {
//base URL
RestAssured.baseURI = "https://run.mocky.io/v3";
String r = RestAssured.given().when()
//get request
.get("/cd3a7e12-9057-4b51-bf2d-a2d4e2ddad8d")
//get response as string
.then().extract().response().asString();
System.out.println(r);
}
}输出

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP