Utilities exposed by ktor

Estimated reading time: 1 minute

Handling URL-encoded properties

Ktor exposes a few extension methods for parsing and generating url-encoded strings.

URL-encoded strings looks like this: param=value&other=hi.

Parsing:

There is an extension method for String that allows to get a parsed Parameters object from it. You can limit the maximum number of parsed parameters with the optional limit parameter.

fun String.parseUrlEncodedParameters(defaultEncoding: Charset = Charsets.UTF_8, limit: Int = 1000): Parameters

Encoding:

Either from a List of Pairs of strings or a Parameters instance, you can generate an url-encoded string.

fun List<Pair<String, String?>>.formUrlEncode(): String
fun List<Pair<String, String?>>.formUrlEncodeTo(out: Appendable)
fun Parameters.formUrlEncode(): String
fun Parameters.formUrlEncodeTo(out: Appendable)