katolsster katolsster - 1 year ago 161
Groovy Question

Groovy: Creating and sending SOAP request from envelope without using wsdl (like curl).

I have a large number of xml strings that I picked up from a database, and I would like to wrap each one of them into SOAP messages and send to a recipient. I want to do this using a groovy script, in the same fashion as I would do it with curl. That means I would like to avoid using a wsdl, but instead wrap the existing xml string body into a soap envelope and then just send it to the address and port of the recipient. Is there any way of doing this with e.g. wslite or any other SOAP api for groovy?

Answer Source

you can use either the HttpBuilder:

HTTPBuilder http = new HTTPBuilder( 'http://some.com' )
http.request( POST ){
  uri.path = '/somepath'
  requestContentType = URLENC
  body = [ your:json, envelope:here ]
  headers.Accept = 'application/json'

  response.success = { resp, json ->
    println json

or plain UrlConnection:

HttpURLConnection connection = new URL( 'http://some.com/somepath' ).openConnection()
connection.requestMethod = 'POST'
connection.doOutput = true
connection.outputStream.withWriter{ it << "{ some:value }" } // here comes your envelop
String result
connection.content.withReader{ result = new JsonSlurper().parseText( it.readLine() ).someKey }
log.info "got result $result"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download