ExactaBox
ExactaBox

Reputation: 3385

Get response body on success in HTTPBuilder-NG

I'm trying to use Httpbuilder-NG in the Gradle script of an Android Studio project. The script uploads some files to a web server for validation, the server responds with 'ok' or the name of the file that did not validate.

I am trying

response.success { FromServer fs, Object body ->
    println "Success: ${fs.statusCode}, Text is: ${body}, Properties are: ${body.properties}"
}

and the result is always:

Success: 200, Text is: [102, 105, 108, 101], Properties are: [class:class [B, length:4]

Note it is a 4-element array, not a text string. And the array stays the same whether the server returns 'ok' or something else. I recognize my server may be returning something non-standard but it works fine in Postman.

I have also tried

    response.success { FromServer fs, Object body ->
        println "has body = ${fs.hasBody}"
        println "content type = ${fs.contentType}"
        println "charset = ${fs.charset}"
        println "files uploaded, result = ${fs.reader.text}"
        //println "Success: ${fs.statusCode}, Text is: ${body}, Properties are: ${body.properties}"
    }

and the result is always

has body = true
content type = text/html
charset = UTF-8
files uploaded, result = 

i.e. a blank string where the body should be.

fs.hasBody returns true

Any help would be appreciated.

Upvotes: 1

Views: 1654

Answers (1)

Mirko Kovacevic
Mirko Kovacevic

Reputation: 13

def httpBin = configure {
        request.uri = 'http://groovy-lang.org/processing-xml.html'
    }
    def result = httpBin.get() {
        response.success {  fromServer,body ->
             body
        }           
    }
    assert result instanceof groovy.util.slurpersupport.NodeChild
    println result

Code snippet above returns all text inside <body> tag of this web page http//...processing-xml

To narrow your result, you need to parse groovy.util.slurpersupport.NodeChild futher.

Upvotes: 1

Related Questions