https://kotlinlang.org logo
#http4k
Title
# http4k
m

Michael Grigoryan

12/29/2021, 5:49 AM
Hey everyone. I am new to http4k and I have a question about transferring JSON information. From what I learned from the documentation, I have to create lens(
BiDiBodyLens<T>
) to send and I guess receive JSON but isn't there any other way of sending JSON as a response in http4k except for I guess "injecting" the response? Also is there any place where I can find all the terminology used in http4k? My code currently looks like this:
Copy code
// Will be used for sending JSON encoded emoji as a response
val lensEmojiResponse: BiDiBodyLens<Emoji> = Body.auto<Emoji>().toLens()

/**
 * Entrypoint of the application. We can consider
 * this as the main router to which all other routes
 * are attached to.
 */
val app: HttpHandler = { _: Request ->
    val emoji = Emoji("Sob", "😭")

    // Setting the response value. It will be already
    // encoded as JSON by kotlinx.serialization
    lensEmojiResponse.inject(emoji, Response(Status.OK))
}
d

dave

12/29/2021, 9:20 AM
I guess you've ready seen the docs on lenses: https://www.http4k.org/guide/reference/json/ What did you mean by "any other way" exactly? There are 2 ways: 1. You can set the body of the message to a string or inputstream using response.body() 2. You can use a lens to inject or extract - which also sets the content type header. Fyi a slightly nicer way of writing it (syntactic sugar) is actually :
Copy code
response.with(lens of value)
A glossary is probably a good idea, but we don't have a dedicated one. There are the concepts and core module reference pages however at the moment
m

Michael Grigoryan

12/30/2021, 11:06 AM
Thanks a lot for your response, David. And yeah adding a glossary page would be really nice. I can help you guys with creating it, if you really need my help, haha!
3 Views