[ResponseObject] Cannot write binary content to file

Use Case

A simple web service request is sent to an endpoint. A binary file is sent in the response. I want to save that file on disk.

Response sample

Here is the response header, which indicates this is a png image.
HTTP/1.1 200
Content-Disposition: attachment; filename=“vds.png”
Content-Length: 1987
Date: Wed, 03 Apr 2019 18:16:51 GMT
Content-Type: image/png

Problem

Trying to write the response content to file using:
response.getBodyContent().writeTo(fileOutputStream)

The problem is that the content is considered text, so this produces an invalid binary file. The bodycontent type is HttpTextBodyContent.

Question

If that is not a bug, can someone provide an example where a reponse binary content is wrote to file.

Can you share the URL of your endpoint which you want to test?

Not really unfortunately.

The content-type is ok: image/png
The body is also ok. The same request works in postman. The only problem is Katalon is that the body is considered text, so encoding messes up the file.

Have you attempted to convert it?
https://api-docs.katalon.com/com/kms/katalon/core/testobject/impl/HttpTextBodyContent.html

What do you mean by converting it ? As soon as Katalon instantiates the body as a HttpTextBodyContent the encoding is pretty much done at that point. Concerting it back to binary will produce an invalid file.

Katalon’s ResponseObject is designed to process JSON/XML text only. It is not designed to process binary response. See https://github.com/katalon-studio/katalon-studio-testing-framework/blob/master/Include/scripts/groovy/com/kms/katalon/core/testobject/ResponseObject.java getResponseBodyContent()

If you want to download a binary dataf from URL in a Test Case of Katalon Studio, you should write a bit of Groovy script. Let me show you an example. Please make a test case with any name, copy & paste the following snippet, and run it. You will get an image file at <projectDir>/temp/avator.png

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

import com.kms.katalon.core.configuration.RunConfiguration

Path projectDir = Paths.get(RunConfiguration.getProjectDir())
Path tmpDir = projectDir.resolve('temp')
Files.createDirectories(tmpDir)
Path output = tmpDir.resolve("avator.png")

URL url = new URL("https://dub2.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/48/333_2.png")

URLConnection connection = url.openConnection()
assert connection != null
println "connection.getContentLength()=" + connection.getContentLength()

InputStream is = new BufferedInputStream(connection.getInputStream())
OutputStream os = new BufferedOutputStream(new FileOutputStream(output.toFile()))

byte[] buffer = new byte[1024]
int n
while ((n = is.read(buffer)) > 0) {
	println "n=" + n
	os.write(buffer, 0, n)
	os.flush()
}

os.close()

Ok, so it essentially bypasses all the object repository plumbing. Will do a change request and in the mean time i will try in a new keyword to find a way to reuse the requests defined in object repository.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.