CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-io-ktor--ktor-server-core-jvm

Core server functionality for the Ktor asynchronous web framework, providing essential building blocks for HTTP servers including application lifecycle management, routing foundations, request/response handling, and plugin architecture specifically compiled for JVM targets.

Pending
Overview
Eval results
Files

request.mddocs/

Request Processing

Request handling functionality for accessing request properties, headers, parameters, cookies, and converting request content to Kotlin types with full type safety.

Capabilities

Application Request

Abstract base class for HTTP requests providing access to request properties, headers, and content processing pipeline.

/**
 * Interface for HTTP requests
 */
interface ApplicationRequest {
    /** The application call this request belongs to */
    val call: ApplicationCall
    /** Pipeline for processing request content */
    val pipeline: ApplicationReceivePipeline
    /** URL query parameters */
    val queryParameters: Parameters
    /** Raw query parameters without decoding */
    val rawQueryParameters: Parameters
    /** Request headers */
    val headers: Headers
    /** Connection information */
    val local: RequestConnectionPoint
    /** Request cookies */
    val cookies: RequestCookies
    /** HTTP method (GET, POST, etc.) */
    val httpMethod: HttpMethod
    /** Request URI */
    val uri: String
    /** HTTP version */
    val httpVersion: String
}

Content Receiving

Type-safe functions for receiving and converting request content to specific Kotlin types using reified generics.

/**
 * Receives and converts request content to specified type
 * @param T - Target type for content conversion
 * @return Converted content instance
 * @throws ContentTransformationException if conversion fails
 */
suspend inline fun <reified T : Any> ApplicationCall.receive(): T

/**
 * Receives request content that may be null
 * @param T - Target type for content conversion  
 * @return Converted content instance or null
 */
suspend inline fun <reified T : Any> ApplicationCall.receiveNullable(): T?

/**
 * Receives request content as plain text
 * @return Request body as String
 */
suspend fun ApplicationCall.receiveText(): String

/**
 * Receives form parameters from request body
 * @return Parameters from form data
 */  
suspend fun ApplicationCall.receiveParameters(): Parameters

/**
 * Receives request content as byte array
 * @return Request body as ByteArray
 */
suspend fun ApplicationCall.receiveBytes(): ByteArray

/**
 * Receives multipart form data
 * @return MultiPartData for processing file uploads and form fields
 */
suspend fun ApplicationCall.receiveMultipart(): MultiPartData

/**
 * Receives content from a specific channel
 * @param T - Target type
 * @return Received content
 */
suspend inline fun <reified T : Any> ApplicationCall.receiveChannel(): T

Request Properties

Access to various request properties including parameters, headers, cookies, and connection information.

/**
 * Connection information for requests
 */
interface RequestConnectionPoint {
    /** Request scheme (http, https) */
    val scheme: String
    /** Request version (HTTP/1.1, HTTP/2) */
    val version: String
    /** Server port */
    val port: Int  
    /** Server host */
    val host: String
    /** Client/remote host */
    val remoteHost: String
    /** Request URI */
    val uri: String
    /** Request method */
    val method: HttpMethod
}

/**
 * Interface for accessing request cookies
 */
interface RequestCookies {
    /** Get cookie value by name */
    operator fun get(name: String): String?
    /** Get all cookies as map */
    fun rawCookies(): Map<String, String>
}

/**
 * HTTP method enumeration
 */
enum class HttpMethod(val value: String) {
    GET("GET"),
    POST("POST"), 
    PUT("PUT"),
    DELETE("DELETE"),
    PATCH("PATCH"),
    HEAD("HEAD"),
    OPTIONS("OPTIONS")
}

/** Extension to access HTTP method from call */
val ApplicationCall.httpMethod: HttpMethod get() = request.httpMethod

/** Extension to access request URI from call */
val ApplicationCall.uri: String get() = request.uri

Request Pipeline

Pipeline system for processing and transforming incoming request content through interceptor chains.

/**
 * Pipeline for processing incoming request content
 */
class ApplicationReceivePipeline : Pipeline<Any, ApplicationCall> {
    companion object {
        /** Transform phase for content transformation */
        val Transform: PipelinePhase = PipelinePhase("Transform")
        /** After phase for post-processing */
        val After: PipelinePhase = PipelinePhase("After")
    }
}

/**
 * Type registry for content conversion
 */
class ContentConverter {
    /** Convert content to target type */
    suspend fun convertForReceive(context: PipelineContext<Any, ApplicationCall>): Any?
}

Parameter Access

Utilities for accessing URL parameters, query parameters, and form parameters with type conversion.

/**
 * Interface for parameter collections
 */  
interface Parameters {
    /** Get single parameter value */
    operator fun get(name: String): String?
    /** Get all values for parameter */
    fun getAll(name: String): List<String>?
    /** Get all parameter names */
    fun names(): Set<String>
    /** Check if parameters are empty */
    fun isEmpty(): Boolean
    /** Check if parameter exists */
    fun contains(name: String): Boolean
    /** Check if parameter has specific value */
    fun contains(name: String, value: String): Boolean
    /** Iterate over parameters */
    fun forEach(body: (String, List<String>) -> Unit)
}

/** Access to URL path parameters */
val ApplicationCall.pathParameters: Parameters get() = parameters

/** Access to query parameters */  
val ApplicationCall.queryParameters: Parameters get() = request.queryParameters

/** Utility extensions for parameter conversion */
fun Parameters.getOrFail(name: String): String = 
    get(name) ?: throw MissingRequestParameterException(name)

fun Parameters.getInt(name: String): Int? = get(name)?.toIntOrNull()
fun Parameters.getLong(name: String): Long? = get(name)?.toLongOrNull()
fun Parameters.getBoolean(name: String): Boolean? = get(name)?.toBooleanStrictOrNull()

Usage Examples:

import io.ktor.server.application.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*

// Receiving JSON content
data class User(val name: String, val email: String, val age: Int)

routing {
    post("/users") {
        val user = call.receive<User>()
        // Process user
        call.respond(HttpStatusCode.Created, user)
    }
    
    // Receiving form parameters
    post("/login") {
        val params = call.receiveParameters()
        val username = params["username"] ?: throw BadRequestException("Missing username")
        val password = params["password"] ?: throw BadRequestException("Missing password")
        
        // Authenticate user
        call.respondText("Login successful")
    }
    
    // Receiving plain text
    post("/webhook") {
        val payload = call.receiveText()
        // Process webhook payload
        call.respond(HttpStatusCode.OK)
    }
}

// Accessing request properties
routing {
    get("/info") {
        val method = call.request.httpMethod.value
        val uri = call.request.uri
        val userAgent = call.request.headers["User-Agent"]
        val host = call.request.local.host
        
        call.respondText("Method: $method, URI: $uri, User-Agent: $userAgent, Host: $host")
    }
    
    // Path parameters
    get("/users/{id}") {
        val userId = call.parameters["id"]?.toIntOrNull() 
            ?: throw BadRequestException("Invalid user ID")
        
        // Fetch user by ID
        call.respondText("User ID: $userId")
    }
    
    // Query parameters
    get("/search") {
        val query = call.request.queryParameters["q"] 
            ?: throw BadRequestException("Missing query parameter")
        val limit = call.request.queryParameters.getInt("limit") ?: 10
        
        // Perform search
        call.respondText("Searching for: $query (limit: $limit)")
    }
}

// File upload handling
routing {
    post("/upload") {
        val multipart = call.receiveMultipart()
        multipart.forEachPart { part ->
            when (part) {
                is PartData.FileItem -> {
                    val fileName = part.originalFileName
                    val bytes = part.streamProvider().readBytes()
                    // Save file
                }
                is PartData.FormItem -> {
                    val fieldName = part.name
                    val fieldValue = part.value
                    // Process form field
                }
                else -> Unit
            }
            part.dispose()
        }
    }
}

Install with Tessl CLI

npx tessl i tessl/maven-io-ktor--ktor-server-core-jvm

docs

application.md

config.md

engine.md

index.md

plugins.md

request.md

response.md

routing.md

tile.json