I am trying to call a get method(HttpGetJson) to return the response value and use it to get the resp(for response code) and json(for content) values from it. But I get valid outputs only when I return resp and json separately from response.success. If I try to return the response object I only get NULL. Is there any way of returning the response.
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
}
You could return a map with both, like return [resp: resp, json: json]
.
IMO you should pass a closure to be executed from response.success
:
static HttpGetJson(String url, String path, Closure success) {
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 = success
}
}
void testGET() {
def url = 'testurl'
def path = 'testpath'
//submit a request through GET
def response1 = HttpUtils.HttpGetJson(url,path) { resp, json ->
println resp
println json
}
}