How to get the raw response and URL from HttpResponseDecorator
Asked Answered
T

3

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
        }
Tabescent answered 19/4, 2013 at 1:47 Comment(0)
S
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!

Silvie answered 30/9, 2013 at 13:29 Comment(2)
When I did the above, I got "java.io.IOException: Attempted read from closed stream."Donatelli
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) and therefore can't be read more than once. By removing the 'watch' in the debugger, it worked as intended.Midge
D
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.

Darice answered 8/1, 2020 at 10:49 Comment(0)
C
-1

Try this:

println response.data

Conan answered 16/2, 2015 at 8:53 Comment(1)
javadox.com/org.codehaus.groovy.modules.http-builder/… That's literally the exact opposite of the raw response.Scirrhous

© 2022 - 2024 — McMap. All rights reserved.