19

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"));
    }

}
hdost
  • 883
  • 13
  • 22
Jatinder Pal Singh
  • 331
  • 1
  • 3
  • 9

6 Answers6

15

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

parishodak
  • 4,506
  • 4
  • 34
  • 48
  • I have tried this approach but I do not get the desired results. I get the following exception - com.jayway.restassured.internal.http.ResponseParseException: OK – Jatinder Pal Singh Jan 22 '16 at 18:18
  • you are not clear with your question. please post error stack trace – parishodak Jan 22 '16 at 18:54
  • if you want to verify response code as 200 OK, try `expect().statusCode(200),log().ifError().given()......` or pick an example of how you want to test response header from this link: https://github.com/jayway/rest-assured/blob/master/examples/rest-assured-itest-java/src/test/java/com/jayway/restassured/itest/java/HeaderITest.java – parishodak Jan 22 '16 at 18:58
  • here is a snippet Caused by: org.apache.http.impl.cookie.DateParseException: Unable to parse the date Fri, 22-01-2026 22:13:16 GMT at org.apache.http.impl.cookie.DateUtils.parseDate(DateUtils.java:118) at org.apache.http.impl.cookie.DateUtils.parseDate(DateUtils.java:79) at org.apache.http.impl.cookie.DateUtils$parseDate.call(Unknown Source) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45) at – Jatinder Pal Singh Jan 22 '16 at 22:22
  • I am not using any dates anywhere in my program – Jatinder Pal Singh Jan 22 '16 at 22:25
  • `ResponseParseException` & `DateParseException` are two different exceptions. you are asking me to shoot in the dark to solve your problem without providing proper context. please read http://stackoverflow.com/help/how-to-ask to get helpful answers faster from community. – parishodak Jan 23 '16 at 04:38
  • those 2 exceptions appear in the same stack trace. – Jatinder Pal Singh Jan 25 '16 at 22:54
  • com.jayway.restassured.internal.http.ResponseParseException: OK at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ... Caused by: org.apache.http.impl.cookie.DateParseException: Unable to parse the date Mon, 25-01-2026 23:11:50 GMT at org.apache.http.impl.cookie.DateUtils.parseDate(DateUtils.java:118) at org.apache.http.impl.cookie.DateUtils.parseDate(DateUtils.java:79) at org.apache.http.impl.cookie.DateUtils$parseDate.call(Unknown Source) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45) at – Jatinder Pal Singh Jan 25 '16 at 23:17
  • @JatinderPalSingh, you have to provide the code you tried, api, response and then exception and your question. just an exception is of no use to decipher the problem you are facing. – parishodak Jan 26 '16 at 04:56
  • also, you are piggybacking a new question on this old question, which seems unrelated to the exceptions you are facing. post a new question explaining your problem with all the details. piggybacking on questions is not encouraged on SO, as it is difficult to find accurate answers for the problems faced by community. – parishodak Jan 26 '16 at 05:00
  • if the answer resolved your problem, pls mark as answer. – parishodak Jan 26 '16 at 05:03
  • May be i was unable to convey the message correctly but the solution of passing header does not seem to be working. i have tried the following ways but none of them worked. – Jatinder Pal Singh Jan 26 '16 at 22:30
  • `given(). header("Id", "abc"). header("name","name"). header("","") ...` – Jatinder Pal Singh Jan 26 '16 at 22:37
10

you can also create and add Map Object of multiple headers as below

    Header h1= new Header("Accept", "*/*");
    Header h2 = new Header("Accept-Language", "en-US");
    Header h3 = new Header("User-Agent", "Mozilla/5.0");
    List<Header> list = new ArrayList<Header>();
    list.add(h1);
    list.add(h2);
    list.add(h3);

    Headers header = new Headers(list);
    request.headers(header);
Karan
  • 443
  • 5
  • 14
1

Or you can use Headers() from RestAssured which support you to add multiple headers at the same time to request.

Headers description

Gergely A.
  • 404
  • 3
  • 6
1

this might help:

Map<String,Object> headerMap = new HashMap<String,Object>();
headerMap.put("first_name", "John");
headerMap.put("last_name", "Watson");
Response response = given()
    .baseUri("http://localhost")
    .basePath("user/details")
    .headers(headerMap)
    .get();
vlatko606
  • 909
  • 1
  • 13
  • 21
0

Replace like below:

@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)
            .and()
            .body("response.code", equalTo("200"));
    }
kdoteu
  • 1,527
  • 1
  • 20
  • 26
0

This is how I used with RequestSpecification and I added two headers.

@Test
public void PostRequest() {
    String appKey = "777";// userID is unique
    RequestSpecification myreq = RestAssured.given();
    myreq.header("Content-Type", "application/json");
    myreq.header("Authorization", "Bearer 777");
    // Create Json Object to store attributes
    JSONObject myjson = new JSONObject();
    myjson.put("app_key", appKey);
    myjson.put("status", "critical")
    // Attach those attributes to Body after convert them in to JsonString
    myreq.body(myjson.toString());
    // Post the request with URL
    Response MyRes = myreq.post("https://api.bigpanda.io/data/v2/alerts");
    int ActualStatuscode = MyRes.getStatusCode();
}
Sameera De Silva
  • 1,722
  • 1
  • 22
  • 41