Content Negotiation and kotlinx.serialization
Configure JSON serialization and deserialize request bodies automatically.
What Is Content Negotiation?
Content negotiation is the HTTP mechanism by which a client and server agree on the format of the response body. The client sends an Accept header; the server picks the best matching format. Ktor's ContentNegotiation plugin automates this.
Adding Dependencies
Add the ContentNegotiation plugin and the kotlinx.serialization JSON converter:
dependencies {
implementation("io.ktor:ktor-server-content-negotiation:2.3.12")
implementation("io.ktor:ktor-serialization-kotlinx-json:2.3.12")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.1")
}All lessons in this course
- Ktor Project Setup: embeddedServer and Application Modules
- Routing and Typed Parameters
- Content Negotiation and kotlinx.serialization
- Authentication Plugins: JWT and Session