In the Gateway, I need to read a parameter from the Request named "Key" from the request body and write it to the logs. I have tried the following approach where I am able to read the body. But, since I already read the body, the Request processing in the service does not go through. Another issue is, the return statement gets executed before the lambda block completes reading the request. How could I delay the return statement till the lambda block is completed?
Approach I used:
private fun getKey(exchange: ServerWebExchange): String? {
val rawRef = AtomicReference<String>()
exchange.request.body.subscribe { buffer ->
val bytes = ByteArray(buffer.readableByteCount())
buffer.read(bytes)
DataBufferUtils.release(buffer)
val jsonObject = ObjectMapper().readTree(Strings.fromUTF8ByteArray(bytes))
rawRef.set(jsonObject.get("key").textValue())
}
return rawRef.get()
}
Any suggestion on how to read the body without disturbing further processing would be helpful.