I have a library that perform http requests to a series of external APIs. This library is a SDK that every other projects can import to use inside.
I am adding a lot of logs inside my library and, in some steps, I would like to log the HttpRequest and HttpResponse from external call. I have tried to log java.net.HttpRequest that was build using HttpRequest.builder() using jackson but I got an empty JSON.
As example, I'll show my code:
fun getFacebookUser(identifier: String): HttpResponse {
val mapper = ObjectMapper()
logger.debug("starting recover a user...")
val httpRequest = buildHttpRequest(...) // internal function that returns a HttpRequest
logger.debug("request coming:", httpRequest) // how to log httpRequest as json?
....more code....
}
I really want to avoid something like:
fun myAwesomeLogFunction(request: HttpRequest) {
logger.debug("request uri", request.uri())
//and goes on for every attribute that I want to log
}
Is there any way to log entire httpRequest
as json?
edit: HttpClient, HttpRequest and all Http stuff comes from java.net.http