'Extract the contents of a plain/text in rest assured
I am getting a response from my RestAssured call as ContentType text/plain;charset=UTF-8.
I searched the internet but am unable to find a nice way to get the content out of the message as using the below is not so nice;
String content = response.then().extract().body().htmlPath().get().children().get(0).toString();
How can I extract the contents of this response a little more nice?
Solution 1:[1]
You can try
String ResponseAsString=given().get("http://services.groupkt.com/state/get/IND/UP").asString(); System.out.println("My ResponseAsString is:"+ResponseAsString);
Also you can extract the response using JsonPath even though it's 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]");
Solution 2:[2]
This is the way it work to me
import io.restassured.response.Response;
response.getBody().prettyPrint();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sameera De Silva |
| Solution 2 | Vladi |
