Skip to main content

Content Starts Here

How to write Base64 encoded data from SOAP response to a file?

Question
How to write Base64 encoded data from SOAP response to a file?
Answer
If the attachment is not being sent in the response as a MIME message, it will not be available on the Attachment tab of the response. For example, you may receive an attachment as Base64 encoded data, please see an example:
SOAP file

In this case, you can write an attachment to a file using the below Groovy script. You will need to add this script to the Groovy test step or to the RequestFilter.afterRequest event handler. Change the bold values so that they could be applied to your case.

import java.io.FileOutputStream

def rawFile = context.expand( '${SOAP Request#Response#declare namespace ns1=\'http://www.pragmaticsw.com/\'; //ns1:GetBodyResponse[1]/ns1:GetBodyResult[1]}' ) //XPath to the base64 data
def decoded = rawFile.decodeBase64()
FileOutputStream fos = new FileOutputStream("D:\\Files\\Attachment.pdf") //specify the path and filename
fos.write(decoded)
fos.finalize()


Tip: you can get the path to the encoded file data using the Get Data dialog. Please see the sample video: https://www.screencast.com/t/5wp3VwoT6qO
Previous MonthNext Month
SunMonTueWedThuFriSat