Hello,
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.
Reason:
java.io.NotSerializableException: com.kms.katalon.core.testobject.ResponseObject
at java_io_ObjectOutput$writeObject.call(Unknown Source)
at TESTCASE - testcase.run(TESTCASE - testcase:42)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:339)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:330)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:309)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:301)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:235)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1605196540670.run(TempTestCase1605196540670.groovy:25)
I understand the error says the object “zipFile” is not serializable. I do not know how to fix this though.
Update:
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.
Update 2:
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.