【问题标题】:How to get the raw response and URL from HttpResponseDecorator如何从 HttpResponseDecorator 获取原始响应和 URL
【发布时间】:2013-04-19 01:47:27
【问题描述】:

HTTP BuilderREST Client 返回一个 HttpResponseDecorator。如何从中获取原始响应(用于记录目的)?

编辑(一些代码可能很方便):

    withRest(uri: domainName) {
        def response = post(path: 'wsPath', query: [q:'test'])
        if (!response.success) {
            log.error "API call failed. HTTP status: $response.status"
            // I want to log raw response and URL constructed here
        }

【问题讨论】:

    标签: groovy httpresponse httpbuilder


    【解决方案1】:

    我一直在做同样问题的噩梦。这是我使用 HTTPBuilder 的解决方案:-

    response.failure = {resp ->
        println "request failed with status ${resp.status}, response body was [${resp.entity.content.text}]"
        return null
    }
    

    希望有帮助!

    【讨论】:

    • 当我执行上述操作时,我得到“java.io.IOException: Attempted read from closed stream.”
    • 我遇到了同样的问题。这是因为我无意中阅读了两次内容。第一个是通过我的 IDE 的调试器。内容不可重复 (HttpClient doc),因此不能多次阅读。通过删除调试器中的“watch”,它按预期工作。
    【解决方案2】:

    我使用了 XmlUtil,它返回打印出来的漂亮的 xml:

        def data = respXml.data
        assert data instanceof groovy.util.slurpersupport.GPathResult
    
        println "${XmlUtil.serialize(data)}"
    

    如果您的数据是来自 groovyx.net.http.HttpResponseDecorator 的解析响应

    希望对你有帮助。

    【讨论】:

      【解决方案3】:

      试试这个:

      println response.data

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-09
      • 2015-03-23
      • 2017-07-09
      • 1970-01-01
      • 2020-09-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多