Utilities exposed by Ktor (URL-Encoding)
URL-encoded strings look like: param=value&other=hi
.
There is an extension method for String
that allows you to get a parsed Parameters
object from it. You can limit the maximum number of parsed parameters with the optional limit
parameter.
Encoding:
You can generate a URL-encoded string either from a List of Pairs of Strings or a Parameters
instance:
fun List<Pair<String, String?>>.formUrlEncode(): String
fun List<Pair<String, String?>>.formUrlEncodeTo(out: Appendable)
fun Parameters.formUrlEncode(): String
fun Parameters.formUrlEncodeTo(out: Appendable)
"error" to "invalid_request",
"error_description" to "client_id is missing"
).formUrlEncode()
You can also construct it from a instance that you can instantiate by using the Parameters.build
builder and then calling the formUrlEncode
extension, or with the parametersOf()
builder method:
The parametersOf
builder has several signatures, and there is Parameters
operator overloading (+
) to join two Parameters
instances:
parametersOf("a" to "b1", "a" to "b2").formUrlEncode()
(parametersOf("a", "b1") + parametersOf("a", "b2")).formUrlEncode()
parametersOf("a", listOf("b1", "b2")).formUrlEncode()
From a normal Map<String, String>
you can also construct a URL-encoded string, but first you will have to create a List from it:
mapOf(
"error" to "invalid_request",
You can also construct it from a Map<String, List<String>>
by flatMapping it first:
In case you want to avoid constructing a whole String with the content and instead want to write to somewhere directly, you can use the formUrlEncodeTo
method.
Responding URL-encoded
You can do something like:
call.respondUrlEncoded("hello" to listOf("world"))
suspend fun ApplicationCall.respondUrlEncoded(vararg keys: Pair<String, List<String>>) =
respondUrlEncoded(parametersOf(*keys))
suspend fun ApplicationCall.respondUrlEncoded(parameters: Parameters) =
respondTextWriter(ContentType.Application.FormUrlEncoded) {
parameters.formUrlEncodeTo(this)