It is supposed to align, but I still get 415 form ...
# ktor
a
It is supposed to align, but I still get 415 form ktor. What am I doing wrong?
e
Which content negotiation module are you using?
For example, the Jackson content negotiation module is set to only parse JSON by default, as seen in the default parameter of its extension function:
If you take a look at https://ktor.io/servers/features/content-negotiation.html, in the “basic usage” section they mention that you register different converters for different content types explicitly, so if you don’t have a converter set up for
ContentType.Application.FormUrlEncoded
you’ll get the 415 back
a
I'm using kotlinx.serialization
serialization { json() }
e
Alright, then you’ve registered a JSON converter in the content negotiator but not a converter for application/x-www-form-urlencoded. The content negotiation feature expects that if you’re just responding with an object that it has a converter which will convert the response into the appropriate type provided by the client’s “accept” header. After following this thread: https://kotlinlang.slack.com/archives/C0A974TJ9/p1531337146000380 it appears that you can sidestep the content negotiation feature by providing the already-serialized response via
call.respondText()
like so:
Copy code
accept(ContentType.Application.FormUrlEncoded) {
            route("/api") {
                get("/test") {
                    println("Got message")
                    // Do stuff
                    val response = listOf("hello" to "world").formUrlEncode()
                    call.respondText(response, ContentType.Application.FormUrlEncoded)
                }
            }
        }
The other option is to provide the content negotiation feature with a serializer that handles URL encoded content and converts it to and from objects
But those aren’t readily available to my knowledge
a
oh wait a sec
the contents of the request are url encoded, not the response
maybe my understanding of this feature is wrong
i want to accept url encoded requests
e
Yeah, you can do that. You just can’t use the Content Negotiation feature to deserialize the form encoded data into an object without a registered converter for that content type. If you want to just read the form encoded data as Ktor’s
Parameters
object, you can do so via:
Copy code
call.receive<String>().parseUrlEncodedParameters()
Also, the
accept()
block you have is fine if you want to just have those endpoints receive form data. The effect will just be that if you try to hit it with a different content type it will respond with a 404 not found
a
thanks 👍