Skip to content
CodeKruPro

Custom Response Headers

26/62

Custom Response Headers

The /headers/custom endpoint returns custom response headers that you can read with response.getHeader():

  • X-App-Version: 1.0
  • X-Request-Id: req-testkru-001
  • Cache-Control: no-cache

Task: Read and print all three custom headers.

Expected output:

X-App-Version: 1.0
X-Request-Id: req-testkru-001
Cache-Control: no-cache

About this lesson

REST Assured: get a response header value with getHeader()

Learn REST AssuredIntermediateHeaders

How it works

Status and body are only part of an HTTP answer. Headers carry metadata such as caching rules, API versions and correlation ids, and many contracts make promises about them. A test that ignores headers will not notice when a deploy drops a Cache-Control directive.

The testkru /headers/custom endpoint sets three of them: an app version, a fixed request id and a cache directive. Its JSON body repeats the same values next to a short message, but the lesson is about reading them from the headers themselves.

On a REST Assured Response, getHeader(name) returns the value of that header as a String, and header(name) is an alias. Names are matched without regard to case, so the lower-case form works as well. A header the server did not send gives null. For assertions, then().header(name, value) or then().header(name, matcher) checks the same thing and fails with an AssertionError that lists every header received.

About the assignment

Call the custom headers endpoint and print the three headers it sets, each on a line labelled with the header name.

What you will do

  1. Send a GET to /headers/custom and keep the Response.
  2. Read X-App-Version with getHeader() and print it after a label made of the header name and a colon.
  3. Do the same for X-Request-Id.
  4. Finish with Cache-Control, in that order.

Common mistakes

  • Reading the body instead of the headers

    The body happens to contain the same values, so a JsonPath read prints the right text while testing the wrong thing. Use the header methods so a missing header is caught.

  • Exact matching on a header with parameters

    Headers like Content-Type often carry a charset. An equalTo() assertion on just the media type fails here. Use containsString(), or the contentType() check from lesson 29.

  • Not handling null

    A misspelled header name returns null instead of throwing. If your code calls a method on the value, the failure surfaces later as a NullPointerException far from the cause.

In a real test suite

Header assertions belong in the shared response specification of a suite: version, caching and security headers are checked on every call rather than in individual tests. Lesson 42 builds that kind of reusable spec.

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: GET /headers/custom
        // TODO: Print response.getHeader("X-App-Version")
        // TODO: Print response.getHeader("X-Request-Id")
        // TODO: Print response.getHeader("Cache-Control")
    }
}