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
- Call
head()on/usersfromgiven()and keep theResponse. - Print the status code after
Status:. - Fetch the
Content-Typeheader withgetHeader(), test whether it is notnulland print that afterHas Content-Type:. - Convert the body with
asString(), checkisEmpty()and print the result afterBody is empty:.
Common mistakes
Reading data from a HEAD response
Code copied from a GET that calls
jsonPath().getList()fails with aJsonPathException. 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-Lengthon its HEAD reply, sogetHeader()returnsnullfor 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.
Related lessons
- OPTIONS RequestOPTIONS, the other verb answered with headers and no content.
- Validate Content-Type with MatchersAsserts on the Content-Type header with the
ContentTypeenum. - Delete a UserChecks for an empty body after a 204 DELETE.
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.
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
}
}