Skip to content
CodeKruPro

HEAD Request

25/62

HEAD Request

HEAD is identical to GET but returns only headers, no body. It's used to check if a resource exists or to read metadata without downloading the content: a missing resource such as /users/99 still answers 404, just without a body.

Task:

  1. Send a HEAD request to /users
  2. Verify you get headers but an empty body

Expected output:

Status: 200
Has Content-Type: true
Body is empty: true

About this lesson

REST Assured HEAD request: response headers without a body

Learn REST AssuredIntermediateDELETE and HTTP Methods

How it works

HEAD is GET with the body removed. The server runs the same lookup and returns the same status and headers, but sends no content. That makes it the cheap way to ask "does this resource exist?" or "what type is it?" without downloading the payload.

testkru implements HEAD by running the GET handler and discarding the output. HEAD /users returns 200 and a Content-Type of JSON with a UTF-8 charset, the same header a GET would carry, while HEAD /users/99 returns 404, also with nothing in the body. So the status code alone tells you whether the user exists.

In REST Assured the verb is head(). Headers come from getHeader(), which returns null when a header is absent, so a not-null check is a quick existence test. The body is an empty string, and reading any value from it through jsonPath() throws a JsonPathException.

About the assignment

Send a HEAD request to the users collection and show three facts: it succeeded, it carries a content type, and no body came with it.

What you will do

  1. Call head() on /users from given() and keep the Response.
  2. Print the status code after Status:.
  3. Fetch the Content-Type header with getHeader(), test whether it is not null and print that after Has Content-Type:.
  4. Convert the body with asString(), check isEmpty() and print the result after Body is empty:.

Common mistakes

  • Reading data from a HEAD response

    Code copied from a GET that calls jsonPath().getList() fails with a JsonPathException. HEAD never returns data; send a GET when you need the users.

  • Relying on Content-Length

    You might expect a length header to tell you the size of the GET body. This server sends no Content-Length on its HEAD reply, so getHeader() returns null for it.

  • Printing the header instead of the check

    The task asks whether the header exists, not what it says. Printing the full value adds the charset text and no longer matches the expected line.

In a real test suite

HEAD is handy in smoke tests and link checkers: a quick loop over URLs that asserts each returns 200 without paying for the bodies. It is also a fast way to confirm caching headers before a full download test.

To try one of these calls on any API endpoint without a task, open the Online Selenium Java Compiler (REST Assured is included).

Starter code

The editor above opens with this code. Fill in the TODO lines, then run and submit it.

Main.java
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import static io.restassured.RestAssured.*;

public class Main {
    public static void main(String[] args) {
        RestAssured.baseURI = "https://testkru.com/api";

        // TODO: Send HEAD request to /users
        // TODO: Print the status code
        // TODO: Check if Content-Type header exists (not null)
        // TODO: Check if body is empty
    }
}