How to pass authorization token in header in Rest assured?

Viewed 42454

Trying to automate api testing using Rest assured

@Test
public void Login() {
    Response resp = given().
            body("{\"phone_number\":\"2222222222\",\"\r\n" + 
                    "               + \" \"country_code\": \"+91\",\"\r\n" + 
                    "               + \" \"login_type\": 0}").
            when().
            contentType(ContentType.JSON).
            post("http://url/api/v1/login");

    System.out.println(resp.asString());
}
2 Answers

First Create Method as httpHeaderManager()

Create an object of Header class for headers and store it into ArrayList e.g

public static Headers httpHeaderManager(){
    Header contentType = new Header("Content-Type","application/json");
    Header authorization = new Header("Authorization", "your token");
    List<Header> headerList = new ArrayList<Header>();
    headerList.add(contentType);
    headerList.add(authorization);
    Headers header = new Headers(headerList);

}

Second call httpHeaderManager() method

@Test
public void create(){
  Response response = 
            given()
            .headers(httpHeaderManager())

}

Related