We can verify JSON response headers in Rest Assured. This is achieved with the help of the header method. Every response obtained from the server may contain zero or more headers that provide metadata about the response.
import org.junit.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.containsString;
public class ResponseHeader {
@Test
public void verifyResponseHeader() {
// Given
given()
// When
.when()
.get("https://reqres.in/api/users/2")
// Then
.then()
.statusCode(200).statusLine("HTTP/1.1 200 OK")
.log().all()
.header("Content-Type" , "application/json; charset=utf-8")
.header("Content-Encoding" , "gzip")
.header("Server" , containsString("cloudflare"));
}
}
The output of the above program is
![](https://qaautomation.expert/wp-content/uploads/2023/12/image-13.png?w=983)
In the above example, we can verify the Content-Type, Server, and Content-Encoding using the header.