Web service : save response as PDF file

We’ve got a web service that return an application/pdf as content-type. We can use the test object with code 200 and see a raw output corresponding to a PDF file. With swagger-ui we’ve got no problem to call it a get the PDF as a result.

Now, we want to save it within a test.

But with Katalon we’re unable to create a valid PDF. We’ve try various way, nothing seems to works. The resulting file can’t be opened («file corrupted» and then ask a password. There’s no password !)

Any ideas, brillant mind of Katalon community ?

ResponseObject response = WS.sendRequest(findTestObject('file_name/pdf', [('BODY_langue') : 'fr']))

byte[] pdfContent = response.getResponseBodyContent().getBytes('ISO-8859-1')

OutputStream outputStream = new FileOutputStream('temp/carnet-vaccination.pdf')
outputStream.write(pdfContent)
outputStream.close()

EDIT: We’ve «AI copiloted» as much as possible, but no suggestions worked.

1 Like

In the <KatalonStudioInstallationDirectory>/Contents/Eclipse/configuration/resources/source directory, you can find the source codes of Katalon Studio. In the com.kms.katalon.core/com.kms.katalon.core-sources.ja of v9.0.0, I could find the source of com.kms.katalon.core.testobject.ResponseObject class. Here I quote the source of getResponseBodyContent() method, as follows:

    /**
     * Get the response body content as a String
     * 
     * @return the response body content as a String
     * @throws Exception if errors happened
     */
    // TODO: Detect the source to see if it is JSON, XML, HTML or plain text
    public String getResponseBodyContent() throws Exception {
        if (responseText != null) {
            if (contentType != null && contentType.startsWith("application/xml")) {
                Matcher SOAPEnvMatcher = SOAPPatternEnvelope.matcher(responseText);
                Matcher SOAPBodyMatcher = SOAPPatternBody.matcher(responseText);
                if (SOAPEnvMatcher.find() && SOAPBodyMatcher.find()) {
                    // SOAP xml
                    DocumentBuilder db = DocumentBuilderProvider.newBuilderInstance();
                    Document doc = db.parse(new InputSource(new StringReader(responseText)));
                    XPath xPath = XPathFactory.newInstance().newXPath();
                    NodeList nodes = (NodeList) xPath.evaluate("/*/*/*", doc, XPathConstants.NODESET);
                    if (nodes.getLength() > 0) {
                        // Body root node always at the last
                        Node lastNode = nodes.item(nodes.getLength() - 1);
                        return nodeToString(lastNode);
                    }
                    return "";
                } else { 
                    // REST xml
                    return responseText;
                }
            } else if (contentType != null && contentType.startsWith("application/json")) {
                return responseText;
            }
            // plain text/html
            else {
                return responseText;
            }
        }
        return "";
    }

You can easily see that this method is written with an assumption that the response body is a java.lang.String type such as JSON, XML, HTML and text/plain.

Does it support PDF? No, it doesn’t. A PDF file is not a string at all. As far as I read the source code, the ReponseObject class is not designed to process any binary files (such as PDF, MS-excel, zip, PNG, JPEG).

If a binary content is provided in the response body, how does the ResponseObject class behave? Does it throw any error saying “I don’t understand this type: application/pdf ? — No. It gives you no warning. It just gives you a strange result. This careless implementation made @Frederic_Canovas1 confused.

This indicates that the Web Service feature of Katalon is not chatted about a lot on the Internet.

If no chats found, AI can not learn anything.

The following sample code shows how to download a binary file from URL using the standard Java API (java.net.* and java.io.*) only.

And the following sample code demonstrates how to use Apache HttpClient in a KS Test Case script.

You would be able to modify this to download and save a PDF from a web service.