简体   繁体   中英

How to store RESTAssured Output to file in JSON Format

I am storing a response from POST call which I make using REST Assured. I want to store response in a file in JSON format, as I will be needing the body next to make a PUT call. Currently I am able to store response to a file, but it stores in String format. How can i convert it to JSON format?

 @Test
public void postIt() throws Exception {
    if(Ver>=currentVer) {
        InputStream resource = getClass().getClassLoader().getResourceAsStream("inputJSONBody.json");
        String json = IOUtils.toString(resource);
        System.out.println(json);
        Response response = given().contentType("application/json").accept("application/json").body(json).when().post("/APIURI");
        String responseBody = response.getBody().asString();
        response.then().statusCode(201);

        try {

            FileWriter file = new FileWriter("./output.json");
            file.write(responseBody);
            file.flush();
            file.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

我能够使用 REST Assured 提供的 prettyPrint() 功能来做到这一点

You can do it with this sample code:

FileWriter file = new FileWriter("./output.json");
file.write(responseBody.prettyPrint());
file.flush();
file.close();

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