Grails Groovy httpbuilder post列表参数
我正在尝试使用grails项目中的web服务。我使用的是httpbuilder 0.7.2。下面是我的http客户端Grails Groovy httpbuilder post列表参数,grails,groovy,httpclient,httpbuilder,Grails,Groovy,Httpclient,Httpbuilder,我正在尝试使用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)
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)
当我在服务器中打印post数据的值时,它只显示列表的最后一个值
{“indexArray”:“5”}
它应该同时显示1和5如果您想使用contenttype应用程序/x-www-form-urlencoded发送json数据,您必须在将数据添加到正文之前显式转换数据,您可以使用(数据为json).如果您想使用contenttype应用程序/x-www-form-urlencoded发送json数据,您必须在将数据添加到正文之前显式转换数据,您可以使用(数据为json)。我使用的是RESTClient(HTTPBuilder上的漂亮方便包装,)。斯波克就这么简单
RESTClient restClient = new RESTClient("http://localhost:8080")
restClient.contentType = ContentType.JSON
它还会自动解析JSON数据,因此我的Spock测试是:
when: "we check the server health"
HttpResponseDecorator response = restClient.get([path : "/health"]) as HttpResponseDecorator
then: "it should be up"
response != null
200 == response.status
'application/json' == response.contentType
我使用的是RESTClient(HTTPBuilder上的便利包装器)。斯波克就这么简单
RESTClient restClient = new RESTClient("http://localhost:8080")
restClient.contentType = ContentType.JSON
它还会自动解析JSON数据,因此我的Spock测试是:
when: "we check the server health"
HttpResponseDecorator response = restClient.get([path : "/health"]) as HttpResponseDecorator
then: "it should be up"
response != null
200 == response.status
'application/json' == response.contentType
为什么要使用
ContentType.URLENC
而不是ContentType.JSON
?没有这个,服务器什么也接收不到为什么要使用ContentType.URLENC
而不是ContentType.JSON
?没有这个,服务器什么也接收不到