Skip to content
CodeKruPro

Validate Content-Type with Matchers

29/62

Validate Content-Type with Matchers

REST Assured provides ContentType enum for validating the Content-Type header:

.then().contentType(ContentType.JSON)  // validates application/json
.then().contentType(ContentType.XML)   // validates application/xml

Task: Validate that /users returns JSON and /xml/users returns XML.

Expected output:

Content-Type validated as JSON
Content-Type validated as XML

About this lesson

REST Assured: validate Content-Type with the ContentType enum

Learn REST AssuredIntermediateHeaders

How it works

The Content-Type response header tells a client how to parse the body. If an endpoint that promised JSON starts returning an HTML error page, every JsonPath call downstream fails with a confusing parse error. Checking the type first turns that into one clear assertion.

REST Assured has a dedicated check: then().contentType(). Given a ContentType constant it compares against the family of media types that constant stands for, and it ignores parameters, which matters because testkru adds a UTF-8 charset to every reply. ContentType.XML covers the XML types, so /xml/users passes it. A plain string such as application/json also works, and so does a Hamcrest matcher.

When the check fails, the AssertionError names the expected type and quotes the actual header in full. Contrast that with then().header("Content-Type", equalTo(...)), which compares the raw string and fails on the charset suffix even when the type is correct.

About the assignment

Assert that the users collection is served as JSON and the XML users endpoint as XML, printing a confirmation after each passing check.

What you will do

  1. GET /users, move to then() and call contentType() with ContentType.JSON.
  2. Print the JSON confirmation line from the starter comment.
  3. GET /xml/users and check it the same way with ContentType.XML.
  4. Print the XML confirmation line. Each print runs only if its assertion passed.

Common mistakes

  • Comparing the header string exactly

    An equalTo() on the raw header fails here because the real value carries a charset parameter. Use contentType(), which understands media types.

  • Swapping the endpoints

    Checking /xml/users against JSON throws an AssertionError that says the JSON type does not match the XML header. Make sure each check points at the endpoint you meant.

  • Printing before validating

    If the confirmation line comes before the then() call, it prints even when the check fails, so the output claims a validation that never happened.

In a real test suite

Teams put expectContentType(ContentType.JSON) in a shared ResponseSpecification so every call in the suite verifies the type without repeating the check. XML endpoints get their own spec, and body assertions such as users.user.size() follow in the same chain.

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 io.restassured.http.ContentType;
import static io.restassured.RestAssured.*;

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

        // TODO: GET /users and validate contentType(ContentType.JSON)
        // TODO: Print "Content-Type validated as JSON"

        // TODO: GET /xml/users and validate contentType(ContentType.XML)
        // TODO: Print "Content-Type validated as XML"
    }
}