Have a method that returns java.io.InputStream for body content


#1

Currently the method in docs ("reponseObject".getBodyContent().getInputStream()) does not work. The method will return null regardless of the type of response given by an API. The reason for this is that not all responses from an API are necessarily text only. Sometimes a response is a binary file (such as an altered image).

I had to resort to Apaches Java framework to get a working InputStream from a response, which kind of defeats the purpose of using Katalon being all inclusive. Having the method proposed in the Katalon Javadoc actually work as described will remove the need of third party support to receive an InputStream for a binary file as a response.


#2

Since this is a request for this feature, and there is no way to likely know if this feature will ever be implemented. This is the solution I had to use, in the event another user needs this as well.

How to get an InputStream as body content and convert to a binary file (such as an image).

import org.apache.http.HttpResponse
import org.apache.http.HttpEntity
import org.apache.http.client.ClientProtocolException
import org.apache.http.client.HttpClient
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.CloseableHttpClient
import org.apache.http.impl.client.HttpClientBuilder
import java.awt.image.BufferedImage
import javax.imageio.ImageIO
import javax.imageio.stream.ImageInputStream
//Shouldn't have to import this, but sometimes Katalon freaks out without it
import java.io.File 

//create your URL
url = "https://${host}${call}${parameters}"

//Create your request methods.HttpPost, methods.HttpPut etc...
HttpGet get = new HttpGet(url)

//pass in authorization header. if needed this following code is for basic authentication
auth = "${user}:${pass}".getBytes()
encodedAuth = Base64.getEncoder().encodeToString(auth)
get.addHeader("Authorization", 	"Basic ${encodedAuth}")

//add additional headers required
get.addHeader("Accept", "image/jpeg")

//create the http client
CloseableHttpClient client = HttpClientBuilder.create().build()

//get your body content as an inputStream and close your client if no longer needed.
stream = client.execute(get).getEntity().getContent()
client.close()

Look at Apaches javaDoc for additional info on the httpclient

The following code will convert a stream to an image. Not required if this does not meet your case
Converting to other files will have similar code. Use Javas BufferedWriter class for most other cases.

ImageInputStream IIS = ImageIO.createImageInputStream(stream)
BufferedImage Bimage = ImageIO.read(IIS)

//write buffered image to file
ImageIO.write(Bimage, "jpg", new File("C:\\Path\\To\\Folder\\filename.jpg"))