RestAssured: How can I get the RequestSpecification fields or parameters after sending a request?

11.5k Views Asked by At

I'm doing cucumber bdd tests i have a class [MyClient] that wraps restassured methods and I have multiple classes that calls [MyClient].

I can do methods like put, post etc. just fine but I am wondering whether there is a way for me to get the actual request fields (header, body...) sent after doing the request.

I also dont have any problems getting and parsing the response but I'm unable to get the request sent.

Considering the following, I can call the sendPostRequest() that will store the RequestSpecification instance to a field called request and I can fetch it anytime by calling the getter. However, I cannot take the individual fields from the RequestSpecification object. From the debugger, I can see the fields just fine so I was thinking there has to be some clean way for me to get it.

I've already tried log() but it doesnt seem to give me what I need. Any help is appreciated!!

CALLING CLASS:

public class MyInterfaceSteps() {
 private myClient = new MyClient();

 public sendPostRequest(){
  myClient.post(someHeaders, someBody, someUrl);
 }
}

CLIENT CLASS:

public class MyClient() {
 private RequestSpecification request;
 private Response response;

 public getRequest() {
  return request;
 }

 public getResponse() {
  return response;
 }

 public Response post(Map<String, String> headers, String body, String url) {
  request = given().headers(headers).contentType(ContentType.JSON).body(body);
  response = request.post(url);  
 }
}
1

There are 1 best solutions below

1
On BEST ANSWER

You create a filter (https://github.com/rest-assured/rest-assured/wiki/Usage#filters) which gives you access to FilterableRequestSpecification (http://static.javadoc.io/io.rest-assured/rest-assured/3.0.3/io/restassured/specification/FilterableRequestSpecification.html) from which you can get (and modify) e.g. headers and body etc. The filter could store these values to a datastructure of your choice.