what can I use in http4k to create a body like thi...
# http4k
e
what can I use in http4k to create a body like this for a POST? >
-d '{"ref":"refs/heads/featureA","sha":"aa218f56b14c9653891f9e74264a383fa43fefbd"}'
f
This from the docs will hopefully help.
j
Copy code
import org.http4k.core.Body
import org.http4k.core.Method
import org.http4k.core.Request
import org.http4k.core.Uri
import org.http4k.core.with
import org.http4k.format.Jackson.json
import org.http4k.format.Jackson.asJsonObject

println(Request(<http://Method.POST|Method.POST>, Uri.of("")).body("""{"ref":"refs/heads/featureA","sha":"aa218f56b14c9653891f9e74264a383fa43fefbd"}"""))

val body =  Body.json().toLens()

println(Request(<http://Method.POST|Method.POST>, Uri.of("")).with(body of mapOf("ref" to "refs/heads/featureA", "sha" to "aa218f56b14c9653891f9e74264a383fa43fefbd").asJsonObject()))
something like those two? - there are quite a few ways, just depending on how you're feeling... - you can of course make DTO types etc...
e
I was hoping for something out of the box and concise the only example in the docs for outgoing is getting injected by a local defined variable
your first example is exactly what I'm doing rn, James the second one I cant get to compile.. what's
.json()
exactly? This?
Copy code
import org.http4k.format.KotlinxSerialization.json
j
i put all the imports in the example above just copy them. ( as sometimes intellij gets confused)
e
sorry, the import optimization or myself messed them up
fixed, now it works
👍 1
I'll write my own shorter version of that, thanks!
j
can it be shorter? 😂
e
lemme try 😛
j
or use a data class of the right shape...
Copy code
import org.http4k.format.Jackson.auto
data class RefSha(val ref: String, val sha: String)
val autoBody = Body.auto<RefSha>().toLens()
println(Request(<http://Method.POST|Method.POST>, Uri.of("")).with(autoBody of RefSha(ref="refs/heads/featureA", sha="aa218f56b14c9653891f9e74264a383fa43fefbd")))
❤️ 1
e
ah, that's what I wanted
👍 1
j
all are valid ways of making the body, they just have different tradeoffs.