WebContext

class WebContext(val call: ApplicationCall)

Context class for handling HTTP requests in Ktor.

This class provides utility methods to access parameters, headers, cookies, and other data from HTTP requests. It also provides methods for responding to requests with JSON or text responses.

Constructors

Link copied to clipboard
constructor(call: ApplicationCall)

Properties

Link copied to clipboard
val call: ApplicationCall

The ApplicationCall instance representing the HTTP request.

Functions

Link copied to clipboard
inline suspend fun <T> body(): T

Parses the request body into an object of type T.

Link copied to clipboard
fun cookie(key: String): String?

Retrieves a cookie value by key.

fun cookie(key: String, defaultValue: String? = null): String?

Retrieves a cookie value by key with a default value if not found.

Link copied to clipboard

Retrieves all cookies as a map of key-value pairs.

Link copied to clipboard
suspend fun error(message: String, status: HttpStatusCode = HttpStatusCode.BadRequest)

Sends an error message in JSON format.

Link copied to clipboard
fun header(key: String): String?

Retrieves a header from the request by key.

Link copied to clipboard

Checks if the HTTP method is DELETE.

Link copied to clipboard
fun isGet(): Boolean

Checks if the HTTP method is GET.

Link copied to clipboard

Checks if the HTTP method is HEAD.

Link copied to clipboard

Checks if the HTTP method is OPTIONS.

Link copied to clipboard

Checks if the HTTP method is PATCH.

Link copied to clipboard

Checks if the HTTP method is POST.

Link copied to clipboard
fun isPut(): Boolean

Checks if the HTTP method is PUT.

Link copied to clipboard
suspend fun json(data: Any?, status: HttpStatusCode = HttpStatusCode.OK)

Sends a JSON response with the provided data.

Link copied to clipboard
suspend fun ok()

Sends an "OK" response with a status code of 200.

Link copied to clipboard
fun param(key: String): String?

Retrieves a parameter from the request by key.

Link copied to clipboard
fun path(index: Int): String?

Retrieves a specific path segment by index.

Link copied to clipboard
inline suspend fun <T : Any> send(data: T, status: HttpStatusCode = HttpStatusCode.OK)

Sends the specified data as a JSON response with a specified status code.