5

I am trying HTTP Post an XML string to a WebMethods server using basic auth. I was trying to use the REST plugin which sits on top of HTTP Builder. I've tried a few things all resulting in a 0 length response. Using Firefox poster I have used the exact same XML and user auth and the WebMethods response is to echo back the request with some extra info, so it is something I am doing in the code below that is wrong. Hope someone has a pointer for doing a HTTP Post of XML.

string orderText = "<item>
  <item>1</item>
  <price>136.000000</price>
</item>"


def response = withHttp(uri: "https://someserver.net:4433") {
      auth.basic 'user', 'pass'

          //  have tried body: XmlUtil.serialize(orderText)
      def r = post(path: '/invoke/document', body: orderText, contentType: XML, requestContentType: XML)
        { resp, xml ->
          log.info resp.status
          log.info resp.data
          resp.headers.each {
            log.info "${it.name} : ${it.value}"
          }
        }
     log.info r
     return r   
}

Logs say:

04-02-2011 14:19:39,894 DEBUG HTTPBuilder - Response code: 200; found handler:    OrdersService$_closure1_closure2_closure3_closure4@36293b29
04-02-2011 14:19:39,895  INFO HTTPBuilder - Status: 200
04-02-2011 14:19:39,896  INFO HTTPBuilder - Data: null
04-02-2011 14:19:39,896  INFO HTTPBuilder - XML: null
04-02-2011 14:19:39,913  INFO HTTPBuilder - Content-Type : application/EDIINT; charset=UTF-8
04-02-2011 14:19:39,913  INFO HTTPBuilder - Content-Length : 0

Cheers,

Steve

Steve
  • 1,457
  • 12
  • 25
  • Could not get this working, even via Groovy HTTPBuilder so went under 1 more layer to Apache HTTPClient, works great – Steve Feb 07 '11 at 01:30
  • Steve, you should post your solution as an answer and accept it. – rochb Feb 11 '11 at 19:59
  • I'm working on the exact same thing, it would be great if you could post your solution. – Slavko Feb 23 '11 at 21:56

3 Answers3

6

Here is what I ended up with. It is quite standard use of common HTTP Client

For basic auth over SSL you can simply have your url like: https://user:pass@www.target.com/etc

Grails remember to copy the HTTPClient jar to the lib folder or in my case I installed the REST plugin which includes HTTPClient anyway.

There are good docs on the HTTPClient site: http://hc.apache.org/httpcomponents-client-ga/

import org.apache.http.HttpEntity
import org.apache.http.HttpResponse
import org.apache.http.client.HttpClient 
import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.DefaultHttpClient

def sendHttps(String httpUrl, String data) {
    HttpClient httpClient = new DefaultHttpClient()
    HttpResponse response
    try {
        HttpPost httpPost = new HttpPost(httpUrl)
        httpPost.setHeader("Content-Type", "text/xml")

        HttpEntity reqEntity = new StringEntity(data, "UTF-8")
        reqEntity.setContentType("text/xml")
        reqEntity.setChunked(true)

        httpPost.setEntity(reqEntity)
        log.info "executing request " + httpPost.getRequestLine()

        response = httpClient.execute(httpPost)
        HttpEntity resEntity = response.getEntity()

        log.info response.getStatusLine()
        if (resEntity != null) {
            log.with {
                info "Response content length: " + resEntity.getContentLength()
                if (isDebugEnabled()) {
                    debug "Response Chunked?: " + resEntity.isChunked()
                    debug "Response Encoding: " + resEntity.contentEncoding
                    debug "Response Content: " + resEntity.content.text
                }
            }
        }
        // EntityUtils.consume(resEntity);
    }
    finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpClient.getConnectionManager().shutdown()
    }
    return response.getStatusLine()
}
Steve
  • 1,457
  • 12
  • 25
2

try it this way:

HTTPBuilder builder = new HTTPBuilder( url )
builder.request( Method.POST ) { 
       // set uriPath, e.g. /rest/resource
       uri.path = uriPath

       requestContentType = ContentType.XML

       // set the xml body, e.g. <xml>...</xml>
       body = bodyXML

       // handle response
       response.success = { HttpResponseDecorator resp, xml ->
           xmlResult = xml
       }
}
raoulinski
  • 344
  • 3
  • 10
  • 1
    Thanks for that, I know I did try something along those lines as the solution is far cleaner...but its a while ago so the details escape me...I will revisit this as I think I may have not used the ContentType.XML – Steve May 02 '12 at 00:15
  • 1
    Thats relatively easy way to do it. above code really helps me, i was stuck like for 2 hrs and above code works in first go. But one thing all the needed jars should be there, and whenever a compile fails, clear script context if you are working within groovy console. – Aamir Feb 18 '15 at 19:49
0

I guess there's no need to get it done that difficult, I use a simpler approach (by the way you don't need extra plugins). So consider the next piece of code, and of course I'm ommiting the authentication part

class YourController{
    static allowedMethods = [operation:['POST','GET']]

    def operation(){
        def xmlRequest = request.reader.text
        println xmlRequest
        //TODO: XML postprocessing, here you might use XmlParser.ParseText(xmlRequest)
    }
}

I know this might be out of context because you are asking for the REST plugin, yet I wanted to share this since there's another alternative.

I'm using grails 2.3.2 and Firefox RESTClient to test the webservice.

darkstar_mx
  • 456
  • 5
  • 8