groovy - 如何获取 Groovy RestClient 失败响应的完整响应

标签 groovy rest-client

目前,我收到 HttpResponseException,它只有 statusCode。 我怎样才能获得完整的回复?

这是我正在使用的代码

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())
    }

它只输出:

[oaf.error] groovyx.net.http.HttpResponseException: Internal Server Error

最佳答案

添加自定义失败响应处理程序:

        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())
        }

关于groovy - 如何获取 Groovy RestClient 失败响应的完整响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28843915/

相关文章:

java - 在 Gradle 设置文件中包含使用通配符的子项目

java - 如何在 Java/Groovy 中开发数据透视表

Kotlin 类似于 Groovy 中的作用域函数(let、also、apply、run)

java - 除非include_type_name设置为true,否则映射定义不能嵌套在类型[_doc]下

java - 您可以使用 Groovy 元编程来覆盖 Java 类上的私有(private)方法吗

spring - 当检查调用时,模拟方法也返回 null

java - 使用 Elastic Search 5.5.0 时如何正确关闭 Raw RestClient 以获得最佳性能?

ruby-on-rails - 如何使用 RestClient 进行 Rails 基本授权?

ruby - 无法安装 restclient gem

ruby - "bad ecpoint"在 OSX Mountain Lion 上安装新的 RVM Ruby 1.9.3 时出现 SSL 错误