https://kotlinlang.org logo
#ktor
Title
# ktor
c

Casey Brooks

11/22/2019, 7:58 PM
Is it possible to use the ktor HTTP client to parse x-www-form urlencoded responses to models, similar to how it parses JSON?
m

mattway

11/23/2019, 9:43 AM
I am also looking for a way to do this
p

Pete

11/24/2019, 9:13 AM
+1 for me, from what I've gathered so far you could use
call.receiveParameters()
to get what is basically a
Map<String, List<String>>
to work with. This doesn't automatically parse into models like the JSON converters. To use the syntax of
call.receive<MyDataModel>()
with
x-www-form-urlencoded
we need to use the
ContentNegotiation
feature and write our own converter to do the mapping:
Copy code
install(ContentNegotiation) {
    register(ContentType.Application.FormUrlEncoded, FormUrlEncodedConverterThatNeedsWriting())
}
I'm knew to Kotlin so I haven't managed to write a converter to handle form data in the same way as something like Ruby on Rails does using
[ ]
as delimiters. I've made this gist from the JacksonConverterTest incase someone is able to help: https://gist.github.com/createdbypete/ac57c6b59133b184799fadbf32b4b93a
4 Views