【发布时间】:2016-05-25 14:31:56
【问题描述】:
我正在尝试调用 get 方法 (HttpGetJson) 来返回响应值并使用它从中获取 resp(用于响应代码)和 json(用于内容)值。但是只有当我与 response.success 分开返回 resp 和 json 时,我才会得到有效的输出。如果我尝试返回响应对象,我只会得到 NULL。有什么方法可以返回响应。
public static Object HttpGetJson(String url, String path)
def http = new HTTPBuilder(url)
//perform a GET request, expecting JSON response
http.request(GET,JSON) { req ->
uri.path = path
//response handler for a success response code
response.success = { resp, json ->
return response//using response instead of json or resp returns null
}
}
public void testGET()
{
def url = 'testurl'
def path = 'testpath'
//submit a request through GET
def response1 = HttpUtils.HttpGetJson(url,path)
println response1
//println response.statusLine.statusCode
}
【问题讨论】:
标签: json rest groovy httpbuilder