Forum Discussion

Intermountain_H_3's avatar
11 years ago

Groovy Code to attach a xml file to the request

Hi all
I have a directory of xml files that I need to attach as attachments to a request
and submit one at a time.


following is the last thing I have tried

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder2 = groovyUtils.getXmlHolder( "documentSubmissionInbound#Request" ) // Get Request body
// get xml files
new File("C://HieGateway_soap//data" ).eachFile{file ->
if(file.name.endsWith(".xml" )){

// clear existing attachments
for( a in holder2.attachments ) {
holder2.removeAttachment( a )
}
holder2.attachFile(file.name,true) // errors here
holder2.contentType = "application/octet-stream"
holder2.setPart( file )
}
}


I will have over 200 of these files so I really dont want to manually add each one at a time
Can u give me advice on what i have wrong if and how this can be done ect
Thanks in advance for your help

1 Reply

  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hi,

    As an FYI SmartBear support does not support custom groovy solutions.

    You should add a RequestFilter.filterRequest event handler and use the following code as an example:


    // clear existing attachments
    for( a in request.getAttachments() ) {
    request.removeAttachment(a)
    }

    // get files and attach
    new File("C:\\temp\\Test Requirements" ).eachFile{file ->
    if(file.name.endsWith(".html" )){
    request.attachFile(file,false)
    }
    }


    This will clear existing attachments and add new attachments in the request.

    Please see the API documentation and event handler documentation.

    http://www.soapui.org/Scripting-Propert ... dlers.html
    http://www.soapui.org/apidocs/pro/


    Regards,
    Marcus
    SmartBear Support