# Web service : save response as PDF file

**URL:** <https://forum.katalon.com/t/web-service-save-response-as-pdf-file/149208>\
**Category:** AI & Agentic Testing\
**Tags:** web-service, copilot, ai\
**Created:** [October 4, 2024, 9:13pm UTC](https://forum.katalon.com/t/web-service-save-response-as-pdf-file/149208 "2024-10-04T21:13:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Frederic\_Canovas1](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/frederic_canovas1/32/20464_2.png) [@Frederic\_Canovas1](https://forum.katalon.com/u/Frederic_Canovas1)\
**Post date:** [October 4, 2024, 9:13pm UTC](https://forum.katalon.com/t/web-service-save-response-as-pdf-file/149208/1 "2024-10-04T21:13:15Z")

</div>

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 ?

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

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [October 4, 2024, 10:21pm UTC](https://forum.katalon.com/t/web-service-save-response-as-pdf-file/149208/2 "2024-10-04T22:21:15Z")

</div>

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:

```auto
    /**
     * 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.

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [October 4, 2024, 10:24pm UTC](https://forum.katalon.com/t/web-service-save-response-as-pdf-file/149208/3 "2024-10-04T22:24:15Z")

</div>

> [@Frederic\_Canovas1](#):
>
> EDIT: We’ve «AI copiloted» as much as possible, but no suggestions worked.

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

If no chats found, AI can learn nothing.

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [October 5, 2024, 3:04am UTC](https://forum.katalon.com/t/web-service-save-response-as-pdf-file/149208/4 "2024-10-05T03:04:22Z")

</div>

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

> [@\[ResponseObject\] Cannot write binary content to file](http://forum.katalon.com/t/responseobject-cannot-write-binary-content-to-file/22454/6):
>
> 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](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 & past…

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

> [@Download the image file via Rest API](http://forum.katalon.com/t/download-the-image-file-via-rest-api/120898/3):
>
> @nithizunnikumaran21 I believe that the WS features of Katalon Studio never work for any type of binary content. You should not use KS for testing the URLs that respond binary content. You should look for other tools such as [Postman](https://www.postman.com/). If you are asked to find a way in KS, you could write test cases that drive the famous Apache HttpClient v4.15 of which jar is bundled in Katalon Studio. You can find some tutorials how to use the Apache HttpClient, like I will show an example Test Case that us…

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

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [October 7, 2024, 12:32pm UTC](https://forum.katalon.com/t/web-service-save-response-as-pdf-file/149208/5 "2024-10-07T12:32:51Z")

</div>

> [@kazurayam](#):
>
> If no chats found, AI can learn nothing.

I communicated with Copilot about this issue. I enjoyed the conversation with Copilot. Let me share it:

* * *

Me

##### Me sent:

Using Katalon Studio, how can I download a PDF file ?

Copilot

###### Copilot replied:

To download a PDF file using Katalon Studio, you can configure the browser’s download preferences to automatically save the file to a specified location. Here’s a step-by-step guide to help you set this up:

1. **Set Browser Preferences** :

- You need to set the browser preferences to handle PDF downloads. This involves configuring the desired capabilities for the browser you are using (e.g., Chrome).

1. **Configure Desired Capabilities** :

- For Chrome, you can set the desired capabilities as follows:

```auto
Map<String, Object> chromePrefs = new HashMap<String, Object>()
chromePrefs.put("download.default_directory", "path/to/download/folder")
chromePrefs.put("download.prompt_for_download", false)
chromePrefs.put("plugins.always_open_pdf_externally", true)

Map<String, Object> chromeOptions = new HashMap<String, Object>()
chromeOptions.put("prefs", chromePrefs)

DesiredCapabilities capabilities = DesiredCapabilities.chrome()
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions)

WebDriver driver = new ChromeDriver(capabilities)

```

1. **Run Your Test** :

- With the above configuration, when you run your test, the PDF file should be downloaded automatically to the specified directory without prompting.

1. **Example Test Case** :

- Here’s a simple example of a test case that navigates to a URL and downloads a PDF file:

```auto
WebUI.openBrowser('')
WebUI.navigateToUrl('http://example.com/pdf-file-url')
WebUI.click(findTestObject('Page_Example/button_DownloadPDF'))
WebUI.delay(10) // Wait for the download to complete
WebUI.closeBrowser()

```

[Make sure to replace `"path/to/download/folder"` with the actual path where you want the PDF to be saved and `'Page_Example/button_DownloadPDF'` with the actual test object for the download button](http://forum.katalon.com/t/how-to-download-a-file-in-katalon/46322)[1](http://forum.katalon.com/t/how-to-download-a-file-in-katalon/46322)[2](http://forum.katalon.com/t/is-this-possible-to-use-katalon-to-download-a-file/46318).

If you encounter any issues or need further customization, feel free to ask!

References

[1 ![]()forum.katalon.com](http://forum.katalon.com/t/how-to-download-a-file-in-katalon/46322)  
[2 ![]()forum.katalon.com](http://forum.katalon.com/t/is-this-possible-to-use-katalon-to-download-a-file/46318)  
[3 ![]()docs.katalon.com](https://docs.katalon.com/katalon-platform/analyze/reports/view-test-reports/view-test-run-results/download-test-report-files)  
[4 ![]()forum.katalon.com](http://forum.katalon.com/t/how-to-verify-downloaded-file/8967)  
[5 ![]()docs.katalon.com](https://docs.katalon.com/katalon-studio/docs/introduction-to-desired-capabilities.html)+2 その他

* * *

##### Me sent:

I want to do that using Web Service Testing feature of Katalon, not by WebUI feature.

Copilot

thinking…

###### Copilot replied:

Got it! To download a PDF file using the Web Service Testing feature in Katalon Studio, you can follow these steps:

1. **Create a Web Service Request** :

- Go to **File \> New \> Web Service Request**.
- Enter the URL of the PDF file you want to download.
- Set the request method to **GET**.

1. **Send the Request and Save the Response** :

- In the **Request Object** tab, configure the request as needed.
- Click on **Send** to execute the request.
- Save the response to a file. You can do this by scripting in the **Script** tab of the Web Service Request.

1. **Script to Save the Response** :

- Here’s an example of how you can save the response to a PDF file:

```auto
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.testobject.ResponseObject
import java.nio.file.Files
import java.nio.file.Paths

// Send the request
ResponseObject response = WS.sendRequest(findTestObject('YourWebServiceRequest'))

// Get the response content
byte[] pdfContent = response.getResponseBodyContent()

// Define the path where you want to save the PDF
String filePath = 'path/to/save/yourfile.pdf'

// Save the PDF file
Files.write(Paths.get(filePath), pdfContent)

```

1. **Run the Test Case** :

- Save and run your test case. The PDF file should be downloaded and saved to the specified location.

Make sure to replace `'YourWebServiceRequest'` with the actual name of your web service request object and `'path/to/save/yourfile.pdf'` with the desired file path.

If you need further assistance or run into any issues, feel free to ask!

* * *

##### Me sent:

It seemed that the getResponseBodyContent method of ResponseObject of Katalon returns a rubbish bytes.

Copilot

generating…

###### Copilot replied:

It sounds like the response content might not be properly handled or encoded. Let’s try a different approach to ensure the PDF content is correctly saved. You can use the `getResponseBodyContentAsStream` method to handle the response as a stream and then write it to a file. Here’s an updated script:

```auto
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.testobject.ResponseObject
import java.nio.file.Files
import java.nio.file.Paths
import java.io.InputStream
import java.io.OutputStream
import java.nio.file.StandardCopyOption

// Send the request
ResponseObject response = WS.sendRequest(findTestObject('YourWebServiceRequest'))

// Get the response content as an InputStream
InputStream inputStream = response.getResponseBodyContentAsStream()

// Define the path where you want to save the PDF
String filePath = 'path/to/save/yourfile.pdf'

// Save the PDF file
OutputStream outputStream = Files.newOutputStream(Paths.get(filePath))
byte[] buffer = new byte[1024]
int bytesRead
while ((bytesRead = inputStream.read(buffer)) != -1) {
    outputStream.write(buffer, 0, bytesRead)
}
inputStream.close()
outputStream.close()

```

This script reads the response content as a stream and writes it to a file, which should help avoid issues with encoding or corrupted bytes.

Make sure to replace `'YourWebServiceRequest'` with the actual name of your web service request object and `'path/to/save/yourfile.pdf'` with the desired file path.

Give this a try and let me know if it works! If you encounter any further issues, feel free to ask.

* * *

##### Me sent:

I believe, the ResponseObject class does not implement a method getResponseBodyContentAsStream() which returns an instance of InputStream. Are you fooling me?

Copilot

thinking…

###### Copilot replied:

I apologize for the confusion. You’re right, the `ResponseObject` class in Katalon Studio does not have a `getResponseBodyContentAsStream()` method. Let’s correct that approach.

Instead, you can use the `getResponseBodyContent()` method to get the response as a byte array and then write it to a file. Here’s the revised script:

```auto
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.testobject.ResponseObject
import java.nio.file.Files
import java.nio.file.Paths

// Send the request
ResponseObject response = WS.sendRequest(findTestObject('YourWebServiceRequest'))

// Get the response content as a byte array
byte[] pdfContent = response.getResponseBodyContent().getBytes("ISO-8859-1")

// Define the path where you want to save the PDF
String filePath = 'path/to/save/yourfile.pdf'

// Save the PDF file
Files.write(Paths.get(filePath), pdfContent)

```

This script should correctly handle the PDF content and save it to the specified file path. Make sure to replace `'YourWebServiceRequest'` with the actual name of your web service request object and `'path/to/save/yourfile.pdf'` with the desired file path.

Give this a try and let me know if it works! If you encounter any further issues, feel free to ask.

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [October 7, 2024, 12:37pm UTC](https://forum.katalon.com/t/web-service-save-response-as-pdf-file/149208/6 "2024-10-07T12:37:59Z")

</div>

I, kazurayam, think that the last code suggested by Copilot would work. Actually, it is the shortest code that works in Katalon Studio. But I would not accept it as a solution. It’s a terrible idea to convert a byte array to a String and then convert the String back to a byte array. Katalon’s ResponseObject is just poorly designed. The API should be improved.

Thanks to Copilot. It suggested what’s missing — `getContentBodyAsInputStream()`

> Copilot replied  
> I apologize for the confusion. You’re right, the `ResponseObject` class in Katalon Studio does not have a `getResponseBodyContentAsStream()` method. Let’s correct that approach.

Copilot quickly learned something out of my comments. Copilot could correct its mistakes. Well, well… I was impressed.

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [October 8, 2024, 11:36am UTC](https://forum.katalon.com/t/web-service-save-response-as-pdf-file/149208/7 "2024-10-08T11:36:53Z")

</div>

I had one more session with Copilot. I enjoyed it. Let me share it.

* * *

I want to download a PDF from web using Katalon Studio. Could you tell me how to ?

## Copilot

Sure! Here’s a step-by-step guide to download a PDF from the web using Katalon Studio:

1. **Open Katalon Studio** and create a new project or open an existing one.
2. **Create a new Test Case** or open an existing one where you want to add the download functionality.
3. **Add a WebUI Open Browser** keyword to open the browser1. You can specify the URL of the webpage that contains the PDF2.
4. **Locate the Download Button** : Use the WebUI Find Element keyword to locate the download button or link on the webpage3.
5. **Click the Download Button** : Use the WebUI Click keyword to click on the download button or link4.
6. **Set Desired Capabilities** : Configure the browser settings to automatically download files to a specific directory without prompting5. For example, for Chrome, you can use the following settings:

java

Copy

```auto
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("download.default_directory", "C:/path/to/download/directory");
chromePrefs.put("download.prompt_for_download", false);

```

1. **Download the File** : Use the WebUI Send Keys keyword to simulate pressing `Ctrl + S` to save the file6. Alternatively, you can use the WebUI Handle Windows Dialog keyword to interact with the Windows Save As dialog box7.

Here’s a sample script to illustrate the process:

java

Copy

```auto
import com.katalon.studio.common.KeywordUtil;
import com.katalon.studio.common.StringUtil;
import com.katalon.studio.exception.KatalonException;
import com.katalon.studio.ui.common.WebUI;
import com.katalon.studio.ui.common.WebUIUtil;
import java.util.HashMap;

// Open the browser
WebUI.openBrowser("https://example.com/download-page");

// Locate the download button and click it
WebUI.waitForElementPresent(findTestObject("DownloadButton"), 10);
WebUI.click(findTestObject("DownloadButton"));

// Set Chrome preferences to download files automatically
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("download.default_directory", "C:/path/to/download/directory");
chromePrefs.put("download.prompt_for_download", false);
WebUI.executeJavaScript("Object.assign(navigator.chrome, chromePrefs);");

// Simulate pressing Ctrl + S to save the file
WebUI.sendKeys(findTestObject("DownloadButton"), Keys.chord(Keys.CONTROL, "s"));

```

Make sure to replace `"https://example.com/download-page"` and `"DownloadButton"` with the actual URL and element locator for your specific case.

Does this help? Let me know if you have any questions or need further assistance!

* * *

## You

I want to do that using Web Service feature of Katalon rather than using WebUI keywords. How can I do it?

## Copilot

Got it! To download a PDF using the Web Service feature in Katalon Studio, you can follow these steps:

1. **Create a new Test Case** or open an existing one.
2. **Add a Web Service Request** : In the Manual View, click on the plus icon to add a new Web Service Request1. You can also do this in the Script View by adding the necessary Groovy code2.
3. **Configure the Web Service Request** : Set up the request to target the URL that provides the PDF3. You’ll need to configure the request method (e.g., GET or POST) and any necessary headers or parameters4.
4. **Send the Request** : Use the `Send Request` keyword to send the web service request5.
5. **Save the Response** : Capture the response from the web service and save it as a PDF file6. You can use Groovy code to handle the response and save it to a file.

Here’s a sample script to illustrate the process:

groovy

Copy

```auto
import com.katalon.studio.core.WebService
import java.nio.file.Files
import java.nio.file.Paths

// Define the URL and other request parameters
def url = 'https://example.com/download-pdf'
def method = 'GET'
def headers = ['Content-Type': 'application/pdf']

// Send the web service request
def response = WebService.sendRequest(url, method, headers)

// Save the response as a PDF file
def filePath = 'C:/path/to/download/directory/response.pdf'
Files.write(Paths.get(filePath), response.body)

```

Make sure to replace the URL, headers, and file path with the actual values for your specific case.

Does this help? Let me know if you have any questions or need further assistance!

* * *

## You

There is no such class com.katalon.studio.core.WebService

## Copilot

I apologize for the confusion. It seems I made a mistake. Katalon Studio uses a different class for handling web services1. You should use the `com.kms.katalon.core.webservice.common.WebServiceMethod` class instead2.

Here’s an updated example using the correct class:

groovy

Copy

```auto
import com.kms.katalon.core.webservice.common.WebServiceMethod
import java.nio.file.Files
import java.nio.file.Paths

// Define the URL and other request parameters
def url = 'https://example.com/download-pdf'
def method = 'GET'
def headers = ['Content-Type': 'application/pdf']

// Send the web service request
def response = WebServiceMethod.sendRequest(url, method, headers)

// Save the response as a PDF file
def filePath = 'C:/path/to/download/directory/response.pdf'
Files.write(Paths.get(filePath), response.body)

```

Does this help? Let me know if you have any questions or need further assistance!

* * *

## You

The com.kms.katalon.core.webservice.common.WebServiceMethod does not implement a method “sendRequest”

## Copilot

I apologize for the confusion. It seems I made another mistake. Let’s try a different approach using the correct Katalon Studio keywords for web services.

Here’s how you can download a PDF using the Web Service feature in Katalon Studio:

1. **Create a new Test Case** or open an existing one.
2. **Add a Web Service Request** : In the Manual View, click on the plus icon to add a new Web Service Request1. You can also do this in the Script View by adding the necessary Groovy code.
3. **Configure the Web Service Request** : Set up the request to target the URL that provides the PDF2. You’ll need to configure the request method (e.g., GET or POST) and any necessary headers or parameters3.
4. **Send the Request** : Use the `Send Request` keyword to send the web service request4.
5. **Save the Response** : Capture the response from the web service and save it as a PDF file5. You can use Groovy code to handle the response and save it to a file.

Here’s a sample script to illustrate the process:

groovy

Copy

```auto
import com.kms.katalon.core.webserver.WebService
import java.nio.file.Files
import java.nio.file.Paths

// Define the URL and other request parameters
def url = 'https://example.com/download-pdf'
def method = 'GET'
def headers = ['Content-Type': 'application/pdf']

// Send the web service request
def response = WebService.sendRequest(url, method, headers)

// Save the response as a PDF file
def filePath = 'C:/path/to/download/directory/response.pdf'
Files.write(Paths.get(filePath), response.body)

```

Make sure to replace the URL, headers, and file path with the actual values for your specific case.

Does this help? Let me know if you have any questions or need further assistance!

* * *

## You

Katalon does not provide such class `com.kms.katalon.core.webserver.WebService`

## Copilot

I apologize for the confusion. It seems I made another mistake. Let’s try a different approach using the correct Katalon Studio keywords for web services.

Here’s how you can download a PDF using the Web Service feature in Katalon Studio:

1. **Create a new Test Case** or open an existing one.
2. **Add a Web Service Request** : In the Manual View, click on the plus icon to add a new Web Service Request. You can also do this in the Script View by adding the necessary Groovy code.
3. **Configure the Web Service Request** : Set up the request to target the URL that provides the PDF. You’ll need to configure the request method (e.g., GET or POST) and any necessary headers or parameters.
4. **Send the Request** : Use the `Send Request` keyword to send the web service request.
5. **Save the Response** : Capture the response from the web service and save it as a PDF file. You can use Groovy code to handle the response and save it to a file.

Here’s a sample script to illustrate the process:

groovy

Copy

```auto
import com.kms.katalon.core.webserver.WebService
import java.nio.file.Files
import java.nio.file.Paths

// Define the URL and other request parameters
def url = 'https://example.com/download-pdf'
def method = 'GET'
def headers = ['Content-Type': 'application/pdf']

// Send the web service request
def response = WebService.sendRequest(url, method, headers)

// Save the response as a PDF file
def filePath = 'C:/path/to/download/directory/response.pdf'
Files.write(Paths.get(filePath), response.body)

```

Make sure to replace the URL, headers, and file path with the actual values for your specific case.

Does this help? Let me know if you have any questions or need further assistance!

* * *

## You

There is no such class `com.kms.katalon.core.webserver.WebService`

## Copilot

I apologize for the confusion. It seems I made a mistake again. Let’s try a different approach using the correct Katalon Studio keywords for web services.

Here’s how you can download a PDF using the Web Service feature in Katalon Studio:

1. **Create a new Test Case** or open an existing one.
2. **Add a Web Service Request** : In the Manual View, click on the plus icon to add a new Web Service Request. You can also do this in the Script View by adding the necessary Groovy code.
3. **Configure the Web Service Request** : Set up the request to target the URL that provides the PDF. You’ll need to configure the request method (e.g., GET or POST) and any necessary headers or parameters.
4. **Send the Request** : Use the `Send Request` keyword to send the web service request.
5. **Save the Response** : Capture the response from the web service and save it as a PDF file. You can use Groovy code to handle the response and save it to a file.

Here’s a sample script to illustrate the process:

groovy

Copy

```auto
import com.kms.katalon.core.webservice.common.WebServiceMethod
import java.nio.file.Files
import java.nio.file.Paths

// Define the URL and other request parameters
def url = 'https://example.com/download-pdf'
def method = 'GET'
def headers = ['Content-Type': 'application/pdf']

// Send the web service request
def response = WebServiceMethod.sendRequest(url, method, headers)

// Save the response as a PDF file
def filePath = 'C:/path/to/download/directory/response.pdf'
Files.write(Paths.get(filePath), response.body)

```

Make sure to replace the URL, headers, and file path with the actual values for your specific case.

Does this help? Let me know if you have any questions or need further assistance!

* * *

## You

There is a class `com.kms.katalon.core.webservice.common.WebServiceMethod"; but it does not implement a method `sendRequest`

## Copilot

I apologize for the confusion. It seems I made another mistake. Let’s try a different approach using the correct Katalon Studio keywords for web services.

Here’s how you can download a PDF using the Web Service feature in Katalon Studio:

1. **Create a new Test Case** or open an existing one.
2. **Add a Web Service Request** : In the Manual View, click on the plus icon to add a new Web Service Request. You can also do this in the Script View by adding the necessary Groovy code.
3. **Configure the Web Service Request** : Set up the request to target the URL that provides the PDF. You’ll need to configure the request method (e.g., GET or POST) and any necessary headers or parameters.
4. **Send the Request** : Use the `Send Request` keyword to send the web service request.
5. **Save the Response** : Capture the response from the web service and save it as a PDF file. You can use Groovy code to handle the response and save it to a file.

Here’s a sample script to illustrate the process:

groovy

Copy

```auto
import com.kms.katalon.core.webservice.common.WebServiceMethod
import java.nio.file.Files
import java.nio.file.Paths

// Define the URL and other request parameters
def url = 'https://example.com/download-pdf'
def method = 'GET'
def headers = ['Content-Type': 'application/pdf']

// Send the web service request
def response = WebServiceMethod.sendRequest(url, method, headers)

// Save the response as a PDF file
def filePath = 'C:/path/to/download/directory/response.pdf'
Files.write(Paths.get(filePath), response.body)

```

Make sure to replace the URL, headers, and file path with the actual values for your specific case.

Does this help? Let me know if you have any questions or need further assistance!

* * *

## You

**You, Die-Hard!**

## Copilot

My fault—I messed up a bit there. 😅 To correctly download a PDF using the Web Service feature in Katalon Studio, you can use the `WS` class. Here’s the proper way to do it:

1. **Create a Web Service Request object** for the specific URL.
2. **Send the request** and capture the response.
3. **Save the response body** to a PDF file.

Here’s how you can do it:

groovy

Copy

```auto
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import java.nio.file.Files
import java.nio.file.Paths

// Define the URL
def url = 'https://example.com/download-pdf'

// Send the request
def response = WS.sendRequest(findTestObject('Object Repository/your_test_object'))

// Save the response as a PDF file
def filePath = 'C:/path/to/download/directory/response.pdf'
Files.write(Paths.get(filePath), response.getResponseBodyContent().bytes)

```

In this script:

- Replace `'Object Repository/your_test_object'` with the correct path to your Web Service request object.
- The response body content is saved directly as a PDF file.

This should work without needing to rely on the UI. Let me know if you hit any snags or if there’s something else I can help with!

* * *

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [October 8, 2024, 11:43am UTC](https://forum.katalon.com/t/web-service-save-response-as-pdf-file/149208/8 "2024-10-08T11:43:23Z")

</div>

> [@kazurayam](#):
>
> **You, Die-Hard!**
> 
> ## Copilot
> 
> My fault—I messed up a bit there. 😅 …

Yes, Copilot actually messed up there. It was fun for me to tease Copilot.

It seems that AI can be successful only if the subject software is well-designed and problem-free. When the subject has any problem (either of design or implementation), AI could hardly propose a satisfactory solution (a work-around).
