我正在尝试使用HTTPBuilder类将XML数据以POST方式发送到一个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 = { /* handle success*/ }
response.failure = { resp, xml -> /* handle failure */ }
}
经检查,我发现请求确实是以XML作为正文发送的。但我对此有三个问题。第一个问题是它省略了经典的xml行:
<?xml version="1.0" encoding="UTF-8"?>
需要放在文档头部的代码,其次内容类型也未设置为:
application/xml
最后,对于XML中的某些元素,我需要设置属性,例如:
<element1 type="something">...</element1>
但我不知道如何按照上述格式实现这一点。有人知道怎么做吗?或者有其他的方法吗?