我正在尝试从grails项目中使用Web服务。我正在使用httpbuilder 0.7.2。以下是我的http客户端。
static def webServiceRequest(String baseUrl, String path, def data,method=Method.GET,contentType=ContentType.JSON){
def ret = null
def http = new HTTPBuilder(baseUrl)
http.request(method, contentType) {
uri.path = path
requestContentType = ContentType.URLENC
if(method==Method.GET)
uri.query = data
else
body = data
headers.'User-Agent' = 'Mozilla/5.0 Ubuntu/8.10 Firefox/3.0.4'
response.success = { resp, json ->
println "response status: ${resp.statusLine}"
ret = json
println '--------------------'
}
}
return ret
}
当我尝试发送如下内容时,问题就来了:
def input = [:]
input['indexArray'] = [1,5]
api调用
def response = webServiceRequest(url,uri,input,Method.POST)
当我在服务器中打印发布数据的值时,它仅显示列表的最后一个值。
{“indexArray”:“5”}
它应该同时显示 1和5
最佳答案
如果要使用contenttype application / x-www-form-urlencoded发送JSON数据,则必须在将数据添加到主体之前显式转换数据,可以使用(数据为JSON)。
关于grails - Groovy httpbuilder发布列表参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42135476/