Groovy/Grails通过HTTP发布XML(使用REST插件)

Groovy/Grails通过HTTP发布XML(使用REST插件),http,grails,post,groovy,Http,Grails,Post,Groovy,我正在尝试使用基本身份验证将XML字符串发送到WebMethods服务器。我试图使用位于HTTPBuilder之上的REST插件。我尝试了一些方法,都得到了0长度的响应。使用Firefox poster,我使用了完全相同的XML和用户身份验证,WebMethods的响应是用一些额外的信息回显请求,因此我在下面的代码中所做的事情是错误的。希望有人有一个做XML HTTP Post的指针 string orderText = "<item> <item>1</ite

我正在尝试使用基本身份验证将XML字符串发送到WebMethods服务器。我试图使用位于HTTPBuilder之上的REST插件。我尝试了一些方法,都得到了0长度的响应。使用Firefox poster,我使用了完全相同的XML和用户身份验证,WebMethods的响应是用一些额外的信息回显请求,因此我在下面的代码中所做的事情是错误的。希望有人有一个做XML HTTP Post的指针

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   
}
干杯


史蒂夫

这就是我的结局。这是普通HTTP客户端的标准用法

对于通过SSL进行的基本身份验证,您只需将url设置为:

Grails记得将HTTPClient jar复制到lib文件夹,或者在我的例子中,我安装了REST插件,其中包含HTTPClient

HTTPClient站点上有好的文档:

试着这样做:

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
       }
}
HTTPBuilder=新的HTTPBuilder(url)
builder.request(Method.POST){
//设置路径,例如/rest/resource
uri.path=uriPath
requestContentType=ContentType.XML
//设置xml正文,例如。。。
body=bodyXML
//处理响应
response.success={HttpResponseDecorator resp,xml->
xmlResult=xml
}
}

我想没有必要那么困难地完成它,我使用了一种更简单的方法(顺便说一句,您不需要额外的插件)。因此,考虑下一段代码,当然我在考虑认证部分

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)
    }
}
我知道这可能是断章取义的,因为你要求的是REST插件,但我想分享这一点,因为还有另一种选择


我正在使用grails 2.3.2和Firefox RESTClient来测试Web服务。

无法实现此功能,即使通过Groovy HTTPBuilder也无法实现。因此,我又在Apache HTTPClient下添加了一层,非常好用。Steve,您应该将您的解决方案作为答案发布并接受它。我正在做同样的工作,如果你能发布你的解决方案,那就太好了。谢谢你,我知道我确实尝试了一些类似的方法,因为解决方案更干净了……但这是很久以前的事了,所以细节我不知道……我会重新讨论一下,因为我想我可能没有使用ContentType.xml这是一种相对简单的方法。上面的代码真的帮了我的忙,我被困了2个小时,上面的代码在FirstGo中起作用。但有一件事,所有需要的JAR都应该在那里,并且每当编译失败时,如果您在groovy控制台中工作,请清除脚本上下文。
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
       }
}
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)
    }
}