How can i catch referer value dynamically changing in request header

969 views Asked by At

I want to add referer value into my code but when i check it from network tab through inspect of Chrome, referer value is not constant, it's always changing. How can i catch and define it in my code ? Is there a way to set the referer as parameter in request header ?

I'm using RestAssure framework by using Java.

   RestAssured.baseURI="https://test-v2-api.mykredit.com/api/customer/light";
    //RestAssured.baseURI="https://test-v2-api.mykredit.com/api/membership";
    RequestSpecification httpRequest2 = RestAssured.given();
    JSONObject requestparam2 = new JSONObject();
    System.out.println(mobilenum.substring(5)); 
    requestparam2.put("pin",mobilenum.substring(5));
    
    httpRequest2.header("Content-Type","application/json; charset=utf-8");
    httpRequest2.body(requestparam2.toJSONString());
    
    //Response response = httpRequest2.request(Method.POST,"/login");
    Response response = httpRequest2.request(Method.POST,"/pinValidate");
    String responseBody = response.getBody().asString();
    System.out.println(responseBody);
    
    int status = response.getStatusCode();
    System.out.println("Status"+" "+status);
    Assert.assertEquals(status, 200);
    
}

referer

1

There are 1 answers

1
kaweesha On

You can use getHeader(String name) method in io.restassured.response (java documentation) to retrieve values from response headers.

Response response = httpRequest2.request(Method.POST,"/pinValidate");
String referer = response.getHeader("referer");

imports;

import io.restassured.response.Response;

You can use the header(String name) method as well.


UPDATE

You can use getHeaders() method in io.restassured.internal.RequestSpecificationImpl (Java documentation) to retrieve request headers.

Response response = httpRequest2.request(Method.POST,"/pinValidate");
String refererValue = ((RequestSpecificationImpl) httpRequest2).getHeaders().get("referer").getValue();

imports;

import io.restassured.internal.RequestSpecificationImpl;