使用Groovy HTTPBuilder POST XML数据 [英] POST XML data with Groovy HTTPBuilder

查看:586
本文介绍了使用Groovy HTTPBuilder POST XML数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试使用HTTPBuilder类将XML数据发布到URL。目前我有:

  def http = new HTTPBuilder('http:// m4m:aghae7eihuph@m4m.fetchapp.com / api / orders / create')
http.request(POST,XML){
body = {
element1 {
subelement'value'
subsubelement {
key'value2'
}
}
}

response.success = {/ *句柄成功* /}
response.failure = { resp,xml - > / *处理失败* /}
}

检查后发现请求确实用XML作为正文。虽然我有3个问题。首先是,它省略了经典的xml代码行:

 <?xml version =1.0encoding =UTF-8 >?; 

必须位于正文的顶部,其次,内容类型未设置为:

  application / xml 

最后,对于XML中的一些元素,我需要设置属性,例如:

 < element1 type =something> ...< / element1> 

但我不知道如何以上述格式来完成此操作。有没有人有一个想法如何?或者可能是另一种方式? 添加XML声明行插入 mkp.xmlDeclaration()

  • 传递 ContentType.XML 作为请求的第二个参数设置 Content-Type 头部至 application / xml 。我看不出为什么这不适合你,但你可以尝试使用一串 application / xml 来代替。

  • 要为元素设置属性,请在标记构建器中使用以下语法: element1(type:'something'){...}

  • 下面是一个例子:

      @Grab(group ='org .codehaus.groovy.modules.http-builder',module ='http-builder',version ='0.5.2')
    导入groovyx.net.http。*

    new HTTPBuilder ('http:// localhost:8080 /').request(Method.POST,ContentType.XML){
    body = {
    mkp.xmlDeclaration()
    元素(attr:'value '){
    foo {
    bar()
    }
    }
    }
    }

    生成的HTTP请求如下所示:

      POST / HTTP /1.1 
    接受:application / xml,text / xml,application / xhtml + xml,application / atom + xml
    Content-Length:71
    Content-Type:application / xml
    Host:localhost:8080
    连接:Keep-Alive
    Accept-Encoding:gzip,deflate

    < b< ?xml version ='1.0'?>
    < element attr ='value'>< foo>< bar />< / foo>< / element>


    I am trying to POST XML data to a URL using the HTTPBuilder class. At the moment I have:

    def http = new HTTPBuilder('http://m4m:aghae7eihuph@m4m.fetchapp.com/api/orders/create')
    http.request(POST, XML) {
    body = {
            element1 {
                subelement 'value'
                subsubelement {
                    key 'value2'
                }
            }
        }           
    
        response.success = { /* handle success*/ }
        response.failure = { resp, xml -> /* handle failure */ }
    }
    

    and upon inspection I see that the request does get made with the XML as the body. I have 3 issues with this though. The first is, it omits the classic xml line:

    <?xml version="1.0" encoding="UTF-8"?>
    

    which has to go at the top of the body, and secondly also the content type is not set to:

    application/xml
    

    Then lastly, for some of the elements in the XML I need to set attributes, for example:

    <element1 type="something">...</element1>
    

    but I have no idea how to do this in the format above. Does anyone have an idea how? Or maybe an alternative way?

    解决方案

    1. To add the XML declaration line insert mkp.xmlDeclaration() at the beginning of your markup.
    2. Passing ContentType.XML as the second parameter to request sets the Content-Type header to application/xml. I can't see why that isn't working for you, but you can try using a string of application/xml instead.
    3. To set attributes on an element, use this syntax in the markup builder: element1(type: 'something') { ... }

    Here's an example:

    @Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.5.2')
    import groovyx.net.http.*
    
    new HTTPBuilder('http://localhost:8080/').request(Method.POST, ContentType.XML) {
        body = { 
            mkp.xmlDeclaration()
            element(attr: 'value') {
                foo { 
                    bar()
                } 
            }
        }
    }
    

    The resulting HTTP request looks like this:

    POST / HTTP/1.1
    Accept: application/xml, text/xml, application/xhtml+xml, application/atom+xml
    Content-Length: 71
    Content-Type: application/xml
    Host: localhost:8080
    Connection: Keep-Alive
    Accept-Encoding: gzip,deflate
    
    <?xml version='1.0'?>
    <element attr='value'><foo><bar/></foo></element>
    

    这篇关于使用Groovy HTTPBuilder POST XML数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆