1

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.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
ExactaBox
  • 3,235
  • 16
  • 27
  • 2
    as a side note, the array/list: `[102, 105, 108, 101]` is ascii for `[f i l e]`, i.e. those are the bytes for the string `"file"`, if you want to see the string you can do `new String(body)`. – Matias Bjarland May 24 '18 at 10:39
  • `new String(body)` was the trick. I had tried `body.toString()` and that was giving me the character array. Also, I was getting `file` for the response because I mis-configured the multipart upload, now fixed. Thank you, you just saved me multiple hours/days and I still doubt I would have gotten the syntax exactly right. – ExactaBox May 24 '18 at 20:36
  • Funny, I had a feeling the array was a list of characters, but I assumed it was complex Unicode or UTF-8. Didn't even think to check ASCII !! – ExactaBox May 24 '18 at 20:40

1 Answers1

1
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.

  • Thanks for the response, I copied this code exactly, however `result` was still an ASCII character number array and the assert failed. Perhaps being wrapped in an HttpTask inside Gradle changes the output format? – ExactaBox May 24 '18 at 20:38
  • Are you using the HttpBuilder-NG Gradle plugin or just the library itself? – cjstehno Jun 04 '18 at 13:48