How To Get The Complete Answer For Groovy Failover Response RestClient

I am currently getting an HttpResponseException that only has statusCode. How can I get the full answer?

Here is the 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 he only outputs this:

 [oaf.error] groovyx.net.http.HttpResponseException: Internal Server Error 
+6
source share
2 answers

Add custom error 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()) } 
+6
source

Actually, you can extract the full answer from the generated exception. For example, if your caught exception is e , and the body of the JSON response should contain a field named myCustomErrorCode , you can check its value by looking at e.response.data.myCustomErrorCode in addition to e.statusCode .

0
source

Source: https://habr.com/ru/post/983291/


All Articles