Skip to main content

Response Getters

SHAFT API Getters

After getting back the REST-Assured response object, we can use the getters to continue working with it when needed.

Get Response Body

Extracts the response body and returns it as a plain string

String body = api.getResponseBody();

Usage

SHAFT.API api = new SHAFT.API("http://api.zippopotam.us/");
api.get("us/90210").perform();
String body = api.getResponseBody();
SHAFT.Validations.assertThat().object(body).contains("Beverly Hills").perform();

Get Response Status Code

Extracts the response status code as integer

int statusCode = api.getResponseStatusCode();

Usage

SHAFT.API api = new SHAFT.API("http://api.zippopotam.us/");
api.get("us/90210").perform();
int statusCode = api.getResponseStatusCode();
SHAFT.Validations.assertThat().number(statusCode).isEqualTo(200).perform();

Get Response Time

Extracts the response time as long

long responseTime = api.getResponseTime();

Usage

SHAFT.API api = new SHAFT.API("http://api.zippopotam.us/");
api.get("us/90210").perform();
long responseTime = api.getResponseTime();
SHAFT.Validations.verifyThat().number(responseTime).isGreaterThanOrEquals(1.1).perform();
SHAFT.Validations.verifyThat().number(responseTime).isLessThanOrEquals(10000).perform();

Get Response JSON Value

Extracts a string value from the response body by parsing the target JSONPath.
* To extract the desired value, please refer to these urls for examples:
You can learn the JSONPath Syntax from here
And test your JSONPath here *

String value = api.getResponseJSONValue("jsonPath");

Usage

SHAFT.API api = new SHAFT.API("https://jsonplaceholder.typicode.com");
api.get("/users").perform();
String value = api.getResponseJSONValue("$[?(@.name=='Ervin Howell')].address.street");
SHAFT.Validations.assertThat().object(value).isEqualTo("Victor Plains").perform();

Get Response JSON Value As List

Extracts the response as list by parsing the target JSONPath.

String value = api.getResponseJSONValueAsList("jsonPath");

Usage

SHAFT.API api = new SHAFT.API("https://jsonplaceholder.typicode.com");
api.get("/todos").perform();
List<Object> value = api.getResponseJSONValueAsList("$[?(@.completed==true)].completed");
for (Object completed : completedList) {
SHAFT.Validations.verifyThat().object(completed.toString()).isEqualTo("true").perform();
}

Get Response XML Value

String value = api.getResponseXMLValue("xmlPath");

Get Response XML Value As List

List<Object> value = api.getResponseXMLValueAsList("xmlPath");