Ответ всегда пустой

Я пытаюсь выполнить простой HTTP-запрос:

      @Grab(group='io.github.http-builder-ng', module='http-builder-ng-apache', version='1.0.4')
import groovyx.net.http.*

HttpBuilder http = HttpBuilder.configure {
    request.uri = 'https://stackoverflow.com'
    request.accept = ['text/html']
}

http.get {
    response.success { FromServer fromServer ->
        println("Got status $fromServer.statusCode $fromServer.message")
        println("Has body: $fromServer.hasBody")
        try {
            List<String> bodyLines = fromServer.reader.withReader { it.readLines() }
            String body = bodyLines.join("\n")
            if (body.empty) {
                println("Body is empty.")
            } else {
                println("Body: $body")
            }
        } catch (Exception e) {
            println("Reading successful response failed. $e")
        }
    }
}

Результат:

      Got status 200 OK
Has body: true
Body is empty.

В чем секрет чтения тела ответа? Крутой 2.5.19.

1 ответ

The response.successобработчик принимаетBiFunction<FromServer,Object>а также, где вторым параметром является объект содержимого тела. Если вы добавите второй параметр в свойsuccessзамыкание, в нем должно быть содержимое тела, например

      response.success { FromServer fromServer, Object body -> 
    // your stuff
}
Другие вопросы по тегам