I perform a GET request to which the response is a zip file. I would like to save that to file to my local machine. How would I go about that?
My code now looks as follows:
def zipFile = WS.sendRequest(findTestObject('<testobject>')) println('Succesfully sent request for zipfile') String filepath = "<filepath>" FileOutputStream fileOut = new FileOutputStream(filepath.zip"); ObjectOutputStream objectOut = new ObjectOutputStream(fileOut); objectOut.writeObject(zipFile); objectOut.close(); System.out.println("The Object was succesfully written to a file");
This creates the following error:
2020-11-12 16:55:48.191 ERROR c.k.katalon.core.main.TestCaseExecutor - Test Cases/TESTCASE - testcase FAILED.
at java_io_ObjectOutput$writeObject.call(Unknown Source)
at TESTCASE - testcase.run(TESTCASE - testcase:42)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
I understand the error says the object “zipFile” is not serializable. I do not know how to fix this though.
I managed to fix the error I think. My code is now as follows:
ResponseObject zipFile = WS.sendRequest(findTestObject('<testobject>')) println('Succesfully sent request for zipfile') String filepath = "<filepath>" File targetFile = new File(filepath + ".zip") OutputStream outputStream = new FileOutputStream(targetFile) zipFile.getBodyContent().writeTo(outputStream)
This leads to corrupt zipfiles however. I thought it might have something to do with the response itself so I tried a manual download of a zipfile through Postman. This lead to a non corrupt zipfile which I could access and extract. Hence I figured there must be something wrong with my script. I have no clue what though.
I managed to fix one problem. I don’t get corrupt zipfiles anymore. I had to specify in the HTTP header what the content type is, ie application/zip. However, the zipfiles I get now are empty. I did not change anything in the code. It is the same as in my previous update.