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.
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.
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.
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.