Sunday, 24 November 2019

RestAssured: Send request headers

‘header’ or ‘headers’ method is used to set request headers.

Example 1
Response response = given().header("Accept", "application/json").when().get("api/v1/employees").thenReturn();

Example 2
Map<String, Object> headers = new HashMap<>();
headers.put("Accept", "application/json");
response = given().headers(headers).when().get("api/v1/employees").thenReturn();

Example 3
Header header = new Header("Accept", "application/json");
Headers headersObj = Headers.headers(header);
response = given().headers(headersObj).when().get("api/v1/employees").thenReturn();

App.java
package com.sample.app;

import static io.restassured.RestAssured.given;

import java.util.HashMap;
import java.util.Map;

import io.restassured.RestAssured;
import io.restassured.http.Header;
import io.restassured.http.Headers;
import io.restassured.response.Response;

public class App {
    private static int requestCount = 0;

    private static void printResponse(Response response) {
        System.out.println("For the " + (++requestCount) + " request");
        String respStr = response.getBody().asString();
        System.out.println(respStr + "\n");
    }

    public static void main(String args[]) {
        RestAssured.baseURI = "http://localhost:8080";

        Response response = given().header("Accept", "application/json").when().get("api/v1/employees").thenReturn();
        printResponse(response);

        Map<String, Object> headers = new HashMap<>();
        headers.put("Accept", "application/json");
        response = given().headers(headers).when().get("api/v1/employees").thenReturn();
        printResponse(response);

        Header header = new Header("Accept", "application/json");
        Headers headersObj = Headers.headers(header);
        response = given().headers(headersObj).when().get("api/v1/employees").thenReturn();
        printResponse(response);
    }
}

Output
For the 1 request
[{"id":1,"firstName":"Deepak","lastName":"Moud"},{"id":2,"firstName":"Srinivasa Rao","lastName":"Gumma"},{"id":3,"firstName":"Purna Chandra","lastName":"Rao"},{"id":4,"firstName":"Madhavi Latha","lastName":"Gumma"},{"id":5,"firstName":"Raghava","lastName":"Reddy"},{"id":6,"firstName":"Ram","lastName":"Gurram"},{"id":7,"firstName":"Gopi","lastName":"Battu"},{"id":8,"firstName":"Gopi","lastName":"Gumma"},{"id":9,"firstName":"Radha","lastName":"Krishna"}]

For the 2 request
[{"id":1,"firstName":"Deepak","lastName":"Moud"},{"id":2,"firstName":"Srinivasa Rao","lastName":"Gumma"},{"id":3,"firstName":"Purna Chandra","lastName":"Rao"},{"id":4,"firstName":"Madhavi Latha","lastName":"Gumma"},{"id":5,"firstName":"Raghava","lastName":"Reddy"},{"id":6,"firstName":"Ram","lastName":"Gurram"},{"id":7,"firstName":"Gopi","lastName":"Battu"},{"id":8,"firstName":"Gopi","lastName":"Gumma"},{"id":9,"firstName":"Radha","lastName":"Krishna"}]

For the 3 request
[{"id":1,"firstName":"Deepak","lastName":"Moud"},{"id":2,"firstName":"Srinivasa Rao","lastName":"Gumma"},{"id":3,"firstName":"Purna Chandra","lastName":"Rao"},{"id":4,"firstName":"Madhavi Latha","lastName":"Gumma"},{"id":5,"firstName":"Raghava","lastName":"Reddy"},{"id":6,"firstName":"Ram","lastName":"Gurram"},{"id":7,"firstName":"Gopi","lastName":"Battu"},{"id":8,"firstName":"Gopi","lastName":"Gumma"},{"id":9,"firstName":"Radha","lastName":"Krishna"}]




Previous                                                    Next                                                    Home

No comments:

Post a Comment