I see that Parameters can be received from the `re...
# ktor
b
I see that Parameters can be received from the
request
, but I’m trying to build and write them as a
respond
o
I don’t think there is anything built in, please file an issue on github.
But I think ContentNegotiation extension is quite easy to build. If client is sending
Accept: application/x-www-form-urlencoded
of course
b
This is the implementation I’m going to try out 🙂
Copy code
call.respond(object : OutgoingContent.ByteArrayContent() {
    override val contentType get() = ContentType.Application.FormUrlEncoded

    override fun bytes(): ByteArray =
        mapOf(
            "error" to "invalid_request",
            "error_description" to "client_id was not valid"
        )
            .map { "${it.key}=${it.value}" }
            .joinToString(separator = "&")
            .toByteArray()
})
o
I think you might need to quote key & value properly
There is a function:
Copy code
fun List<Pair<String, String?>>.formUrlEncode(): String
Also
fun Parameters.formUrlEncode(): String
So, you may use something like
body = parametersOf("a", "1").formUrlEncode()
Or rather
Copy code
parametersOf(
            "error" to "invalid_request",
            "error_description" to "client_id was not valid"
        ).formUrlEncode()
b
Much better!
Copy code
override fun bytes(): ByteArray = parametersOf(
    "error" to listOf("invalid_request"),
    "error_description" to listOf("client_id was not valid")
).formUrlEncode().toByteArray()
o
Since
formUrlEncode
returns a String, and String will automatically be sent as bytes, you don’t need custom OutgoingContent
You can just a make a function something like
respondUrlEncoded
which will have similar signature to
parametersOf
and encode them
So you will just use
Copy code
call.respondUrlEncoded(
    "error" to listOf("invalid_request"),
    "error_description" to listOf("client_id was not valid")
)
1
b
I kind of like the idea of having that take in a
Parameters
so it would abstract where they were received from and it doesn’t seem to add much at the call site
Copy code
call.respondUrlEncoded(parametersOf(
    "error" to listOf("invalid_request"),
    "error_description" to listOf("client_id was not valid")
))
o
Yep, this works too.