Rest-assured. Is it possible to extract value from request json?

Jay picture Jay · Jan 16, 2014 · Viewed 106.9k times · Source

I'm getting response this way:

Response response = expect().statusCode(200).given().body(requestBody).contentType("application/json")
.when().post("/admin");
String responseBody = response.getBody().asString();

I have a json in responseBody:

{"user_id":39}

Could I extract to string using rest-assured's method only this value = 39?

Answer

Johan picture Johan · Jan 22, 2014

You can also do like this if you're only interested in extracting the "user_id":

String userId = 
given().
        contentType("application/json").
        body(requestBody).
when().
        post("/admin").
then().
        statusCode(200).
extract().
        path("user_id");

In its simplest form it looks like this:

String userId = get("/person").path("person.userId");