Cookie

annotation class Cookie(val name: String, val value: String = KTORGEN_DEFAULT_NAME, val maxAge: Int = 0, val expiresTimestamp: Long = -1, val domain: String = "", val path: String = "", val secure: Boolean = false, val httpOnly: Boolean = false, val extensions: Array<Cookie.PairString> = [])

Add a cookie to the request

This annotation is modeled from HttpMessageBuilder.cookie, but adapted to annotation restrictions:

  • null is not allowed → special values are used to represent absence:

    expiresTimestamp = -1L → no expiration.

    domain = "" and path = "" → no value assigned.

  • The extensions field is defined as Array<PairString> for added security and type-safety when mapping cookie extensions.

Example usage:

@Cookie(
name = "session_id",
value = "abc123", // required in function
maxAge = 3600,
expiresTimestamp = 1735689600000, // 01/01/2025 00:00:00 GMT
secure = true,
httpOnly = true,
extensions = [Cookie.PairString("SameSite", "Strict")],
)
@GET
suspend fun myRequest(@Cookie("yummy_cookie") flavor: String): SecureResponse

/* Generated for function:
cookie(
name = "session_id",
value: "abc123",
maxAge = 3600,
expires = GMTDate(timestamp = 1735689600000),
domain = null, // empty is null
path = null, // empty is null
secure = true,
httpOnly = true,
extensions = mapOf("SameSite" to "Strict"),
)
*/

See also

Types

Link copied to clipboard
object Companion

Contain most used cookie name

Link copied to clipboard
@Target(allowedTargets = [])
annotation class PairString(val first: String, val second: String)

Represent a pair key/value, equivalent a Map

Link copied to clipboard
object SameSites

Values of Cookie with name SameSite

Properties

Link copied to clipboard

Domain to which the cookie applies, empty or blank if not applicable

Link copied to clipboard

Timestamp in milliseconds for the expiration date, or -1 to indicate null

Link copied to clipboard

List of extensions as key/value pairs

Link copied to clipboard
val httpOnly: Boolean = false

Indicates whether the cookie should not be accessible via JavaScript

Link copied to clipboard
val maxAge: Int = 0

Time to live in seconds (0 for a session cookie)

Link copied to clipboard

Name of the cookie

Link copied to clipboard

Path to the cookie, empty or blank if not applicable

Link copied to clipboard
val secure: Boolean = false

Indicates whether the cookie should only be sent over HTTPS

Link copied to clipboard

Value of the cookie (applied to parameter, is obtained of it, else is function is required)