# Response Validations

Validate API responses — status codes, JSON values, response body, schema matching, and response time using SHAFT Engine.

Canonical HTML: https://shafthq.github.io/docs/reference/actions/API/Response_Validations
Guide index: https://shafthq.github.io/llms.txt

## SHAFT API Response Validations
Call `assertThatResponse()` for a hard assertion or `verifyThatResponse()` for a soft verification. Each response condition runs immediately.

### Body
Validate on the response body.
Chains to JSON/body validation methods, including structural JSON equality that ignores array/object ordering.

```java
api.assertThatResponse().body().contains("data");
api.assertThatResponse().body()
 .equalsIgnoringOrder("{\"roles\":[\"admin\",\"tester\"]}");
```
#### Usage
```java
SHAFT.API api = new SHAFT.API("http://api.zippopotam.us/");
api.get("us/90210");
api.assertThatResponse().body().contains("Beverly Hills");
```

### Extracted Json Value
Validate an extracted value from the response body by parsing the target **JSONPath**.

:::info
You can learn the JSONPath syntax from the [JSONPath documentation](https://github.com/json-path/JsonPath) and test your expressions at [jsonpath.com](http://jsonpath.com/).
:::

Chains to [Object validation methods](../Validations#object-validations) to continue building your validation.

```java
api.assertThatResponse().extractedJsonValue("$.data").isEqualTo("data");
```
#### Usage
```java
SHAFT.API api = new SHAFT.API("https://jsonplaceholder.typicode.com");
api.get("/users");
api.assertThatResponse().extractedJsonValue("$[?(@.name=='Chelsey Dietrich')].id").isEqualTo("5");
```

### Extracted Json Value As List
Validate an extracted value from the response body by parsing the target **JSONPath** as a list and check every item against it.

Chains to [Object validation methods](../Validations#object-validations) to continue building your validation.

```java
api.assertThatResponse().extractedJsonValueAsList("jsonPath").isEqualTo("data");
```
#### Usage
```java
SHAFT.API api = new SHAFT.API("https://jsonplaceholder.typicode.com");
api.get("/todos");
api.verifyThatResponse().extractedJsonValueAsList("$[?(@.completed==true)].completed").contains(true);
```

### Time
Validate on the response time.
Chains to [Number validation methods](../Validations#number-validations) to continue building your validation.

```java
api.assertThatResponse().time().isEqualTo(expectedNumberValue);
```
#### Usage
```java
SHAFT.API api = new SHAFT.API("http://api.zippopotam.us/");
api.get("us/90210");
api.verifyThatResponse().time().isGreaterThanOrEquals(100);
api.verifyThatResponse().time().isLessThanOrEquals(100000);
```

### Is Equal To File Content
Validate if the content of the provided actual response object is equal to the expected file content.
```java
api.assertThatResponse().isEqualToFileContent("fileRelativePath");
```

### Does Not Equal File Content
Validate if the content of the provided actual response object is not equal to the expected file content.
```java
api.assertThatResponse().doesNotEqualFileContent("fileRelativePath");
```

### Is Equal To File Content Ignoring Order
Validate if the content of the provided actual response object is equal to the expected file content while ignoring Order of the json objects.
```java
api.assertThatResponse().isEqualToFileContentIgnoringOrder("fileRelativePath");
```

### Does Not Equal File Content Ignoring Order
Validate if the content of the provided actual response object is not equal to the expected file content while ignoring Order of the json objects.
```java
api.assertThatResponse().doesNotEqualFileContentIgnoringOrder("fileRelativePath");
```

### Contains File Content
Validate if the content of the provided actual response object contains the expected file content.
```java
api.assertThatResponse().containsFileContent("fileRelativePath");
```

### Does Not Contain File Content
Validate if the content of the provided actual response object does not contain the expected file content.
```java
api.assertThatResponse().doesNotContainFileContent("fileRelativePath");
```

### Matches Schema
Validate if the content of the provided actual response object matches the schema for the expected file content.
```java
api.assertThatResponse().matchesSchema("fileRelativePath");
```

### Does Not Match Schema
Validate if the content of the provided actual response object does not match the schema for the expected file content.
```java
api.assertThatResponse().doesNotMatchSchema("fileRelativePath");
```

## Response value builders 

These `api.assertThatResponse()` / `api.verifyThatResponse()` methods read a value from the completed response and return a validation builder:

| Method | Value |
| --- | --- |
| `statusCodeValue()` | Response status code as a number. |
| `bodyValue()` | Whole response body. |
| `headerValue(String name)` | One response header. |
| `cookieValue(String name)` | One response cookie. |
| `jsonValue(String path)` | One JSONPath result. |
| `jsonValues(String path)` | A JSONPath list result. |
| `responseTimeMillis()` | Response time in milliseconds. |
| `matchesContract(String fileRelativePath)` | Validates the response against an order-insensitive JSON contract file. |

```java
api.assertThatResponse().statusCodeValue().isEqualTo(200);
api.assertThatResponse().jsonValue("$.name").isEqualTo("SHAFT");
```

## Related

- [Request Builder](/docs/reference/actions/API/Request_Builder)
- [API Authentication](/docs/reference/actions/API/API_Authentication)
- [API](/docs/testing/api)
