2

I'm using REST-Assured in Java and here's how I'm getting my response object:

Response response = RestAssured.given().contentType(ContentType.JSON).header(header_name).get();

I want to know if there's any way to extract the method name used (GET in this case) from the response object.

Akshay Maldhure
  • 787
  • 2
  • 19
  • 38
  • Why would you try to get the _request_ method from the _response_? – Thomas Apr 04 '17 at 09:33
  • About the only way you can possibly do that is by accessing response metadata. And even then, I doubt that it is guaranteed to contain anything of that sort. – M. Prokhorov Apr 04 '17 at 09:54
  • @Thomas I need it for logging purposes. – Akshay Maldhure Apr 04 '17 at 10:06
  • @M.Prokhorov I checked nearly all the methods available for the `response` object, but none of them help. How do you suggest to access the response metadata? – Akshay Maldhure Apr 04 '17 at 10:07
  • I'm not sure you correctly understood my intent: since you're after the request method you should look into the `Request` object and not the response. – Thomas Apr 04 '17 at 11:02
  • @Thomas I got that, but I do not really have anything like a `Request` object in my code, I'm storing my response in a `Response` object. Hence the question. – Akshay Maldhure Apr 04 '17 at 11:43
  • I don't know REST-Assured but from the documentation it seems that since you call `.get()` in the end this will result in a GET request. SInce this seems to be client code and since the client needs to know whether it was a GET request or anything else I doubt you can extract that information from the response. – Thomas Apr 04 '17 at 12:23

1 Answers1

1

Incase if you're interested in knowing the requested method say GET or POST, below code will print the method on the console

given().log().method()
        .when()
        .get("https://www.google.co.in/").then().statusCode(200);

Hope this helps

k10
  • 62
  • 1
  • 7
  • Awesome! That works for me, thanks a lot! Just wondering if it's possible to route the log to log4j logger object. – Akshay Maldhure Apr 12 '17 at 04:10
  • Answering my question above, I wrote a wrapper class which takes a log4j logger as constructor argument and offers a PrintStream whose flush() method writes the written content to the supplied log4j logger. More details at: http://stackoverflow.com/a/33388510/6676240. I modified the `myStringBuilder` to a `String` object and added a check inside `flush()` method as below: `if(!myStringBuilder.equals("")) logger.info(this.myStringBuilder.toString());` – Akshay Maldhure Apr 19 '17 at 09:40