9

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?

Jurn
  • 796
  • 1
  • 8
  • 19
  • Whats the actual response content? If the content type is `text/plain` why are you using `htmlPath`? Are you getting html content but with content type of `text/plain`? – SudhirR Nov 25 '19 at 09:36
  • It is a plaintext containing for example `ABCDE` so no html it is indeed a `text/plain` – Jurn Nov 26 '19 at 15:49

3 Answers3

13

You can directly use .asString() to get the body content of the response irrespective of this return Content-Type.

You can try something like that:

response.then().extract().body().asString();

or directly:

response.asString();
Saurabh
  • 301
  • 2
  • 3
2

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]");
Sameera De Silva
  • 1,722
  • 1
  • 22
  • 41
0

This is the way it work to me

import io.restassured.response.Response;

response.getBody().prettyPrint();
Vladi
  • 1,662
  • 19
  • 30