如何从HttpResponseDecorator获取原始响应和URL

时间:2013-04-19 01:47:28

标签: groovy httpresponse httpbuilder

REST ClientHTTP Builder会返回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
        }

3 个答案:

答案 0 :(得分:11)

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

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

希望有所帮助!

答案 1 :(得分:0)

我使用了XmlUtil,它返回了漂亮的xml:

    def data = respXml.data
    assert data instanceof groovy.util.slurpersupport.GPathResult

    println "${XmlUtil.serialize(data)}"

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

希望有帮助。

答案 2 :(得分:-2)

试试这个:

println response.data