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 = ""
andpath = ""
→ no value assigned.The
extensions
field is defined asArray<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"),
)
*/
Content copied to clipboard