放心提取一个plain/text的内容
Extract the contents of a plain/text in rest assured
我从我的 RestAssured 调用中收到了作为 ContentType text/plain;charset=UTF-8
的响应。
我在互联网上进行了搜索,但无法找到从消息中获取内容的好方法,因为使用下面的方法不太好;
String content = response.then().extract().body().htmlPath().get().children().get(0).toString();
如何才能更好地提取此响应的内容?
您可以直接使用 .asString() 来获取响应的正文内容,而不考虑此 return Content-Type。
您可以尝试类似的方法:
response.then().extract().body().asString();
或直接:
response.asString();
你可以试试
String ResponseAsString=given().get("http://services.groupkt.com/state/get/IND/UP").asString();
System.out.println("My ResponseAsString is:"+ResponseAsString);
您也可以使用 JsonPath 提取响应,即使它是 ContentType.TEXT
Response response=given().contentType(ContentType.TEXT).get("http://localhost:3000/posts");
//we need to convert response as a String and give array index
JsonPath jsonPath = new JsonPath(response.asString());
String title = jsonPath.getString("title[2]");
String author=jsonPath.getString("author[2]");
这就是我的工作方式
import io.restassured.response.Response;
response.getBody().prettyPrint();
我从我的 RestAssured 调用中收到了作为 ContentType text/plain;charset=UTF-8
的响应。
我在互联网上进行了搜索,但无法找到从消息中获取内容的好方法,因为使用下面的方法不太好;
String content = response.then().extract().body().htmlPath().get().children().get(0).toString();
如何才能更好地提取此响应的内容?
您可以直接使用 .asString() 来获取响应的正文内容,而不考虑此 return Content-Type。
您可以尝试类似的方法:
response.then().extract().body().asString();
或直接:
response.asString();
你可以试试
String ResponseAsString=given().get("http://services.groupkt.com/state/get/IND/UP").asString(); System.out.println("My ResponseAsString is:"+ResponseAsString);
您也可以使用 JsonPath 提取响应,即使它是 ContentType.TEXT
Response response=given().contentType(ContentType.TEXT).get("http://localhost:3000/posts");
//we need to convert response as a String and give array index
JsonPath jsonPath = new JsonPath(response.asString());
String title = jsonPath.getString("title[2]");
String author=jsonPath.getString("author[2]");
这就是我的工作方式
import io.restassured.response.Response;
response.getBody().prettyPrint();