REST Assured Tutorial 13 – Writing Response in a Text File
Last updated on December 12th, 2019 at 02:59 pm
As a part of End to End REST Assured Tutorial , in this post We will learn to write response to an external text file in Rest Assured.
You may required to save API response in to an external file like text or JSON etc file for future reference or as a proof. In this post, we will concentrate on saving response in a text file.
Rest Assured provides below methods to get response as:-
- As byte array :- asByteArray()
- As input stream :- asInputStream()
- As string :- asString()
All above methods output can be used to write in to text file.
Rest Assured Code:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
package RestfulBooker.extractResponse; import java.io.File; import java.io.IOException; import java.io.InputStream; import com.google.common.io.Files; import io.restassured.RestAssured; import io.restassured.http.ContentType; import io.restassured.response.Response; import io.restassured.specification.RequestSpecification; public class WriteResponseInTextFile { public static void main(String[] args) throws IOException { // There is no need to add escape character manually. Just paste string within double // quotes. It will automatically add escape sequence as required. String jsonString = "{\"username\" : \"admin\",\"password\" : \"password123\"}"; // Create a request specification RequestSpecification request= RestAssured.given(); // Setting content type to specify format in which request payload will be sent. // ContentType is an ENUM. request.contentType(ContentType.JSON); //Adding URI request.baseUri("https://restful-booker.herokuapp.com/auth"); // Adding body as string request.body(jsonString); // Calling POST method on URI. After hitting we get Response Response response = request.post(); // Getting response as a string and writing in to a file String responseAsString = response.asString(); // Converting in to byte array before writing byte[] responseAsStringByte = responseAsString.getBytes(); // Creating a target file File targetFileForString = new File("src/main/resources/targetFileForString.txt"); // Writing into files Files.write(responseAsStringByte, targetFileForString); // Getting response as input stream and writing in to a file InputStream responseAsInputStream = response.asInputStream(); // Creating a byte array with number of bytes of input stream (available() method) byte[] responseAsInputStreamByte = new byte[responseAsInputStream.available()]; // Reads number of bytes from the input stream and stores them into the byte array responseAsInputStreamByte. responseAsInputStream.read(responseAsInputStreamByte); // Creating a target file File targetFileForInputStream = new File("src/main/resources/targetFileForInputStream.txt"); // Writing into files Files.write(responseAsInputStreamByte, targetFileForInputStream); // Directly getting a byte array byte[] responseAsByteArray = response.asByteArray(); // Creating a target file File targetFileForByteArray = new File("src/main/resources/targetFileForByteArray.txt"); // Writing into files Files.write(responseAsByteArray, targetFileForByteArray); } } |
Output:-
Refresh the project folder if you are using Eclipse. You should see three files as below:-

Open a file you should see it has stored response as below:-

You may be thinking which way is more optimal if you want to write in to a file. If you notice when we get response as String or inputStream , we must need to convert them to Byte array before writing in to a file. In third way we are directly getting Byte Array. So third way is more optimal.
You can clone/download example repo here.
If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning
You can find all Selenium related post here.
You can find all API manual and automation related posts here.
You can find frequently asked Java Programs here.
This is the best thing about your tutorial, you tell concepts like books that too in a noticeably short and precise manner.
Thanks, keep up the excellent work. Will use asByteArray always