Download the image file via Rest API

With Katalon Studio’s WS-features, you can not download and save a binary content into file appropriately.

You can read the source of com.kms.katalon.core.testobject.ResponseObject at

If you read the code, you would easily find that ResponseObject naively assumes that all HTTP response is a text. It does not work properly when the HTTP response contains any binary.


I would show you an evidence.

Here is a sample target URL of an image

I can open this in a browser manually, and save the image into disk. I checked the file length:

$ ls -la | grep apple
-rw-r--r--@   1 kazuakiurayama  staff       60428  2 23 09:21 a-bite-in-the-apple.png

The apple image has the size of 60K bytes. This is correct.

Now I wrote a Test Case in Katalon Studio, tried to save the image into file.

import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject

import com.kms.katalon.core.testobject.ResponseObject
import com.kms.katalon.core.testobject.HttpBodyContent
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS

import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths

/**
 * This Test Case tries to download a image of Apple, save it into a local file.
 * The image is accessible at
 *     https://kazurayam.github.io/myApple/a-bite-in-the-apple.png
 * This script uses the build-in keywords of Katalon Studio for WebService testing.
 */

ResponseObject res = WS.sendRequest(findTestObject('Object Repository/AppleImage'))

assert res.getStatusCode() == 200

HttpBodyContent bodyContent = res.getBodyContent()
println "${bodyContent.getContentLength()} bytes"
println "${bodyContent.getContentType()}"
println "${bodyContent.getContentEncoding()}"   // returns null.
Path file = Paths.get("./apple.png")
Files.createDirectories(file.getParent())
OutputStream ostream = new FileOutputStream(file.toFile())

bodyContent.writeTo(ostream)

assert Files.exists(file)
assert file.toFile().length() > 0

The script wrote the file apple.png. I checked the file size:

$ ls -la | grep apple
-rw-r--r--@   1 kazurayam  staff  107545  2 23 09:15 apple.png

The apple.png has 107K bytes of size. This is wrong.

Why the different file size?

I tried to open the apple.png by the image viewer software on Mac OS, it told me the PNG file is broken.

Clearly, Katalon Studio has a bug. No, I should rather say, KS has a design fault.

@Elly_Tran