Search code examples
javarest-assuredextentreports

How to log RestAssured Request and Response information log in ExtentReports logs


I am using rest-assured lib and extent report for reporting,

ValidatableResponse reponseManualLead = given().header("Accept", "application/json")
                .contentType("application/json").header("Authorization", access_token)
                .body(requestBody).log().body().when()
                .put(losdevUrl + "/endUrl").then().log().all();

log().all() : Pritn log of request/reponse in console log , I need that to be log in extentreport , can we do that.

ExtentTestManager.getTest().log(LogStatus.INFO, "Response is:<br>" + "here");

Solution

  • The recommended usage to include JSON request/response is via MarkupHelper as in the documentation: http://extentreports.com/docs/versions/4/java/#markup-helpers-codeblock

    Markup m = MarkupHelper.createCodeBlock(json, CodeLanguage.JSON);
    test.pass(m);
    
    // shorthand
    test.pass(MarkupHelper.createCodeBlock(json, CodeLanguage.JSON));
    

    The code block will be prettified and display like so:

    enter image description here