这里的数据是XML内容 如何在java或groovy中的HttpBuilder请求体中发送XML数据
def http = new HTTPBuilder(baseUrl)
http.request(Method.POST, ContentType.XML)
{
uri.path = path
headers.'Accept' = 'application/xml'
headers.'Authorization' = "Basic YWU1N2UwYmFiYjUwNGJkM2I5MzI3NzQ3ODkyMjE0Yjk"
headers.'Content-Type' = 'application/xml; charset=utf-8'
body:data
response.success = { resp, reader ->
print "================" + reader.text
}
}
编辑当我尝试'正文:XML内容'
时会出现此例外情况 Error groovyx.net.http.HttpResponseException: Unsupported Media Type
| Error at groovyx.net.http.HTTPBuilder.defaultFailureHandler(HTTPBuilder.java:652)
| Error at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
| Error at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
| Error at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
| Error at java.lang.reflect.Method.invoke(Method.java:601)
| Error at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1276)
| Error at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
| Error at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
| Error at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1086)
| Error at groovy.lang.ExpandoMetaClass.invokeMethod(ExpandoMetaClass.java:1110)
| Error at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:953)
| Error at groovy.lang.ExpandoMetaClass.invokeMethod(ExpandoMetaClass.java:1110)
| Error at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:910)
| Error at groovy.lang.Closure.call(Closure.java:411)
| Error at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:508)
| Error at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:441)
| Error at groovyx.net.http.HTTPBuilder.request(HTTPBuilder.java:390)
| Error at groovyx.net.http.HTTPBuilder$request.call(Unknown Source)
| Error at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
答案 0 :(得分:1)
ContentType
应更改为XML
,并且不会发送body
。试试:
def http = new HTTPBuilder(baseUrl)
http.request(Method.POST, ContentType.XML) {
uri.path = path
headers.'Accept' = 'application/xml'
headers.'Authorization' = "Basic YWU1N2UwYmFiYjUwNGJkM2I5MzI3NzQ3ODkyMjE0Yjk"
body: //put xml body here
//query:data not sure it this is needed, basically there are query params
response.success = { resp, reader ->
print "================" + reader.text
}
}
答案 1 :(得分:1)
def http = new HTTPBuilder(baseUrl)
// perform a POST request
http.request(Method.POST) {
uri.path = path
***requestContentType = ContentType.XML***
headers.'Authorization' = "Basic YWU1N2UwYmFiYjUwNGJkM2I5MzI3NzQ3ODkyMjE0Yjk"
headers.'Accept' = 'application/xml'
headers.'Content-Type' = 'application/xml; charset=utf-8'
body = query // here Query is the XML content
// response handler for a success response code
response.success = { resp, reader ->
//Handle response here
}
response.failure = { resp ->
//Handle failure here
}
}
答案 2 :(得分:0)
在Grails中试试这个:
import grails.converters.*
…
render(contentType: "text/xml") {
text:yourData as XML
}