head() method in rest assured

Sameera De Silva
2 min readJul 5, 2023

--

head() method is used to send an HTTP HEAD request to a specified URL or endpoint.
The HEAD method is part of the HTTP protocol and is similar to the GET method, but it retrieves only the headers of the response, not the actual body content.
The head() method is typically used when you want to check the availability or retrieve metadata about a resource without transferring the entire response body.
It can be useful in scenarios where you need to quickly obtain information such as the content type, last modified date, or server details.(If it’s come with request )

The head() method is a synchronous method, which means that it will block until the request has been sent and the response has been received.

import io.restassured.RestAssured;
import io.restassured.http.Header;
import io.restassured.http.Headers;
import io.restassured.response.Response;
import org.testng.Assert;

import static io.restassured.RestAssured.head;

public class Temp {
public static void main(String[] args) {
RestAssured.baseURI = "https://stackoverflow.com/";
RestAssured.basePath = "/questions/7999258/";
Response response = head("/get-the-last-modified-date-of-an-url");

System.out.println("Response time of Head request is " + response.getTime());

// Get all the headers using getHeaders() method
Headers headers = response.getHeaders();

// Iterate over the headers using a for loop
for (Header header : headers) {
String headerName = header.getName();
String headerValue = header.getValue();
// System.out.println(headerName + ": " + headerValue);
}

// We can add assertions to header values.
// Get the "Connection" header value
String connectionHeaderValue = response.getHeader("Connection");
Assert.assertEquals(connectionHeaderValue, "keep-alive", "Connection header value mismatch");

// Get the "Cache-Control" header value
String cacheControlHeaderValue = response.getHeader("Cache-Control");
Assert.assertEquals(cacheControlHeaderValue, "private", "Cache-Control header value mismatch");
}


}

--

--

No responses yet