Hi sorry for the dumb question I’ve looked into th...
# http4k
t
Hi sorry for the dumb question I’ve looked into the https://www.http4k.org/documentation/, can you use
Copy code
data class SomeRequestModel(
    val account: String
)
to make this request? localhost.com/account=1 Or do I really have to write “account” manually?
Copy code
val someRequestModel =..
request = Request(Method.GET, ...)
.query("account", someRequestModel.account)
s
@ted I'm not sure how your
SomeRequestModel
would map to the query parameters you provided as example, but here's another example:
Copy code
import org.http4k.core.Method
import org.http4k.core.Request
import org.http4k.core.with
import org.http4k.lens.Query

fun main() {
    data class AccountId(val id: String)

    val queryLens = Query.map(::AccountId, AccountId::id).multi.required("account")

    //injecting
    val ids = listOf(AccountId("1"), AccountId("2"))
    val request = Request(Method.GET, "/")
        .with(queryLens of ids)
    println(request.uri) // prints /?accounts=1&accounts=2

    //extracting
    val extractedIds = queryLens(request)
    println(extractedIds) // prints [AccountId(id=1), AccountId(id=2)]
}
Is that close to what you need?
t
woops sorry I’ve updated the request
wow this is awesome
s
Then it's even simpler. You don't need the
.multi
and can use the
map
to map a String to your specific request model
d
No, this doesn't exist - you need to have an idea of which parameters would go into queries, which are form parameters etc... 🙂
t
ah so you really need to write “account”
Copy code
val queryLens = Query.map(::AccountId, AccountId::id).multi.required("account")
s
Updated example for your updated example:
Copy code
import org.http4k.core.Method
import org.http4k.core.Request
import org.http4k.core.with
import org.http4k.lens.Query

fun main() {
    data class SomeRequestModel(val accountId: String)

    val queryLens = Query.map(::SomeRequestModel, SomeRequestModel::accountId).required("account")

    //injecting
    val request = Request(Method.GET, "/")
        .with(queryLens of SomeRequestModel("12354"))
    println(request.uri) // prints /?account=12354

    //extracting
    val extractedIds = queryLens(request)
    println(extractedIds) // prints SomeRequestModel(accountId=12354)
}
t
thanks guys, super fast response. I didn’t expect it 🙂
s
so you really need to write “account”
Yes, we are explicit about that kind of thing, so that code refactoring won't accidentally change how messages will look like.
t
I see, thanks again! coming from a swift background so I kind of expected it to be in the model