HELENDOUDOU
HELENDOUDOU

Reputation: 53

How to get the request header with restassured

I am trying to get the request header before sending the request. Is there an easy way to get the request header directly? Tried something like requestSpecification.log().all() but it will print all the details. Or should I use something like this "RequestLoggingFilter"?

Upvotes: 1

Views: 2486

Answers (1)

Alexey R.
Alexey R.

Reputation: 8676

You can use that by casting your request specification to FilterableRequestSpecification.

Do not forget to check if it implements mentioned interface.

public static void main(String[] args) {
    RequestSpecification header = RestAssured
            .given()
            .header("myHeader", "myValue")
            .header("myHeader2", "myValue2");
    if(header instanceof FilterableRequestSpecification){
        FilterableRequestSpecification spec = (FilterableRequestSpecification)header;
        System.out.println(spec.getHeaders());
    }
}

Type check is required because while RestAssured.given() returns RequestSpecification the only implementation in default package also implements RequestSpecification but in some kinda heavy-customized cases it might not be true..

However given approach will likely work in 99.99% of cases.

Upvotes: 2

Related Questions