10

The REST Client of HTTP Builder returns a HttpResponseDecorator. How can I get the raw response out of it (for logging purposes)?

EDIT (some code might be handy):

    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
        }
zoran119
  • 10,657
  • 12
  • 46
  • 88

3 Answers3

18

I've been having a nightmare with the same problem. Here's my solution using HTTPBuilder:-

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

Hope that helps!

mekondelta
  • 993
  • 7
  • 17
  • 9
    When I did the above, I got "java.io.IOException: Attempted read from closed stream." – Shinta Smith Jul 31 '15 at 20:04
  • I had the same issue. It was caused because I was inadvertently reading the content twice. The first was through my IDE's debugger. Content is not repeatable ([HttpClient doc](http://hc.apache.org/httpcomponents-core-4.2.x/httpcore/apidocs/org/apache/http/HttpEntity.html#getContent%28%29)) and therefore can't be read more than once. By removing the 'watch' in the debugger, it worked as intended. – Luke Jan 02 '19 at 03:16
0

I used XmlUtil, it returns the pretty printed xml:

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

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

If your data is a parsed response from groovyx.net.http.HttpResponseDecorator

Hope it helps.

Sanhaji Omar
  • 192
  • 1
  • 8
-1

Try this:

println response.data

devanon
  • 161
  • 1
  • 1
  • 12
  • 2
    http://javadox.com/org.codehaus.groovy.modules.http-builder/http-builder/0.6/groovyx/net/http/HttpResponseDecorator.html#getData() That's literally the exact opposite of the raw response. – Charles Wood Jun 27 '16 at 22:10