如何将RESTAssured Output以JSON格式存储到文件中

时间:2021-09-10 14:04:38

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?

我正在使用REST Assured来存储POST调用的响应。我想将响应存储在JSON格式的文件中,因为我需要在旁边进行PUT调用。目前我能够存储对文件的响应,但它以String格式存储。如何将其转换为JSON格式?

 @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();
        }

    }

}

1 个解决方案

#1


0  

I was able to do it using prettyPrint() feature offered by REST Assured

我能够使用REST Assured提供的prettyPrint()功能来完成它

#1


0  

I was able to do it using prettyPrint() feature offered by REST Assured

我能够使用REST Assured提供的prettyPrint()功能来完成它