Javalin: How to get resultString in requestLogger

Created on 21 Dec 2018  路  2Comments  路  Source: tipsy/javalin

Describe your question


javalin version: 2.4.0

ctx.resultString method return empty string, when I want to get result at requestLogger.

so, I'm using it like below

app.after { ctx ->
    ctx.attribute("resultString", ctx.resultString())
}

app.requestLogger { ctx, timeMs ->
    println("ctx.attribute: ${ctx.attribute<String>("resultString")}")
}

Is this ok?
Why can't take resultString at requestLogger?

QUESTION

All 2 comments

@paz-raon In Javalin when you call ctx.result(string), it's converted to an input stream internally:

fun result(resultString: String) = result(resultString.byteInputStream(responseCharset()))

When writing the response, this inputstream is consumed. You should be able to get the resultString if you call ctx.resultStream().reset() first.

@paz-raon I added a reset() to the resultString() method, so in the next version of Javalin you won't have to call reset() manually: https://github.com/tipsy/javalin/commit/f6c8d61bbff3656de90b642fb2fca1690bbd9a66.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

TobiasWalle picture TobiasWalle  路  3Comments

maxemann96 picture maxemann96  路  5Comments

JosefEvAlloc picture JosefEvAlloc  路  5Comments

jonerer picture jonerer  路  4Comments

vikascn picture vikascn  路  4Comments