简体   繁体   中英

Get rest-assured request specifications as string

I am trying to get all request details (request url, proxy, header, body etc) as a string since I need to redirect this to a logback log file. I have declared a common base class where I have declared the request variable (io.restassured.specification.RequestSpecification)

I use this base class in the default constructor for my stepDef classes. Each stepDef class has its only instance of log. I have tried using the solutions here: How to get rest-assured log into something printable in a text file but haven't succeeded

I've tried the following solutions:

  1. RestAssured.config = RestAssured.config().logConfig(new LogConfig(baseClass.loggerPrintStream.getPrintStream(), false));

  2. baseClass.request.config(RestAssured.config().logConfig(new LogConfig(baseClass.loggerPrintStream.getPrintStream(),true))).log().all();

  3. baseClass.request.filter(new RequestLoggingFilter(baseClass.loggerPrintStream.getPrintStream()));

None of the above seems to have done the trick for me. All I want is to get the complete request specification (baseClass.request) as a string so I can write the string to log file with a few more details.

If you have RequestSpecification you can use QueryableRequestSpeficiation to get the data from the request like this:

        RequestSpecification requestSpecification = given()
            .auth()
                .preemptive()
                    .basic("username", "password")
            .contentType("application/json")
            .header("x-api-key", "some api key");
        QueryableRequestSpecification queryable = SpecificationQuerier.query(requestSpecification);
        System.out.println("Header is: " + queryable.getHeaders().get("x-api-key"));

Instead of printing the specifications to the console, you could save it to file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM