Rish Rish - 5 months ago 55
Groovy Question

How to export pdf attachment from soap response using groovy?

Although soap (free version) has an option to export document generated in response. Is there any groovy function to extract application/pdf file and store in my local folder ?

enter image description here

Rao Rao

The following script should be able to save the attachment to a file.

Add the below script as Script Assertion to the current request step. Find the appropriate comments inline.

Source for the script is taken from here

* Below script assertion will check 
* if the response is not null
* there is an attachment
* and saves file to the specified location, by default saves into system temp
* directory
//change file name as needed
def fileName = System.getProperty('java.io.tmpdir')+'/test.pdf'

//Get the response and check if the response is not null
def response = messageExchange.response
assert null != response, "response is null"

//Create output stream
def outFile = new FileOutputStream(new File(fileName))

//Check if there is one attachment in the response
assert 1 == messageExchange.responseAttachments.length, "Response attachments count not matching"
def ins = messageExchange.responseAttachments[0]?.inputStream
if (ins) {
   //Save to file
   com.eviware.soapui.support.Tools.writeAll( outFile,  ins )