How do we pass multiple headers in rest assured?

Jatinder Pal Singh picture Jatinder Pal Singh · Jan 22, 2016 · Viewed 44.8k times · Source

I am new to rest-assured and Java, I am trying to do a very basic test of checking the response is 200 ok for API. can you any one please tell me what do I need to change in the below script in order to pass multiple headers Id, Key and ConId?

import org.junit.Test;
import com.jayway.restassured.*;
import com.jayway.restassured.http.ContentType;
import static org.hamcrest.Matchers.*;
import static com.jayway.restassured.RestAssured.*;

public class APIresponse

{

    public static void main(String[] args) 
        {
            APIresponse apiresponse = new APIresponse();
            apiresponse.response();
        }

    @Test
    public void response ()
    {
        baseURI="http://testme/api/";
        given().
            header("Id", "abc"). 
            param("Key", "NuDVhdsfYmNkDLOZQ").
            param("ConId", "xyz").
        when().
            get("/uk?Id=DT44FR100731").
        then().
            contentType(ContentType.JSON).
            body("response.code", equalTo("200"));
    }

}

Answer

parishodak picture parishodak · Jan 22, 2016

Simplest way to add multiple headers is to just repeat .header(headername,headervalue) multiple times after .given()

given().
  header("Id", "abc").
  header("name","name").
  header("","")
  ...

You can find different ways of passing headers using REST-Assured framework in its test suite at this github link.

Edit:

To verify response status in Rest-Assured:

expect().statusCode(200),log().ifError().given()...... 

or pick an example of how you want to test response header from this github link