Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get complete response for Groovy RestClient failed response

Currently, I'm getting HttpResponseException, which has only statusCode. How can I get complete body of response?

Here is code I'm using

restClient = new RESTClient("http://${Server}")
try {
    HttpResponseDecorator resp = restClient.post(path,body,requestContentType)     
        as HttpResponseDecorator
    return JSONObject.fromObject(resp.getData()).get("topKey","");
    }
catch (HttpResponseException e) {
            error(e.toString())
    }

And it only output this:

[oaf.error] groovyx.net.http.HttpResponseException: Internal Server Error
like image 209
Vyacheslav Avatar asked Mar 03 '15 23:03

Vyacheslav


1 Answers

Add custom failed response handler:

        restClient = new RESTClient("http://${Server}")
        restClient.handler.failure = { resp, data ->
            resp.setData(data)
            String headers = ""
            resp.headers.each {
                headers = headers+"${it.name} : ${it.value}\n"
            }
            throw new HttpResponseException(resp.getStatus(),"HTTP call failed. Status code: ${resp.getStatus()}\n${headers}\n"+
                                            "Response: "+(resp as HttpResponseDecorator).getData())
        }
like image 91
Slavik Avatar answered Oct 22 '22 09:10

Slavik