gpt4 book ai didi

java - 如何在groovy中的HttpBuilder请求正文中发送XML数据

转载 作者:行者123 更新时间:2023-11-30 03:31:20 25 4
gpt4 key购买 nike

这里的数据是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
}
}

编辑当我尝试“body: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)

最佳答案

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

关于java - 如何在groovy中的HttpBuilder请求正文中发送XML数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28937309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com