Skip to main content
Version: 9.2605.x.x RR

Cookie

Class functions

bcx.servlet.http.Cookie.parse(cookieAsInSetCookieHeader)

Instantiates a cookie from a string with the same syntax as a Set-Cookie header.

Sample:

local cookie = bcx.servlet.http.Cookie.parse(response:getHeader("Set-Cookie"))

bcx.servlet.http.Cookie.new(name, value)

Instantiates a cookie with the given name and value.

Sample:

local cookie = bcx.servlet.http.Cookie.new("LuaCookie", "SomeValue")

Object methods

cookie:getName()

Returns the name of the cookie.

Sample:

local name = cookie:getName()

cookie:getValue()

Returns the value of the cookie. If the value is quoted, the quotes are returned as well.

Sample:

local value = cookie:getValue()

cookie:setValue(valueAsString)

Sets the value of the cookie.

Sample:

cookie:setValue("MyNewValue")

cookie:getComment()

Returns the comment of the cookie.

Sample:

local comment = cookie:getComment()

cookie:setComment(commentAsString)

Sets the comment of the cookie.

Sample:

cookie:setComment("MyNewComment")

cookie:getDomain()

Returns the domain of the cookie.

Sample:

local domain = cookie:getDomain()

cookie:setDomain(domainAsString)

Sets the domain of the cookie.

Sample:

cookie:setDomain("MyNewDomain")

cookie:getPath()

Returns the path of the cookie.

Sample:

local path = cookie:getPath()

cookie:setPath(pathAsString)

Sets the path of the cookie.

Sample:

cookie:setPath("MyNewPath")

cookie:getExpires()

Returns the expiry date of the cookie.

Sample:

local expires = cookie:getExpires()

cookie:setExpires(expiresAsString)

Sets the expiry date of the cookie. The system does not verify whether the passed string contains a valid date. To ensure correct date formatting, use setMaxAgeAndExpires() instead.

Sample:

cookie:setExpires("Thu, 16-Feb-2017 15:45:32 GMT")

cookie:isHttpOnly()

Returns true if the HttpOnly flag is set.

Sample:

local httpOnly = cookie:isHttpOnly()

cookie:setHttpOnly(boolean)

Sets or removes the HttpOnly flag of the cookie.

Sample:

cookie:setHttpOnly(true)

cookie:isSecure()

Returns true if the Secure flag is set.

Sample:

local secure = cookie:isSecure()

cookie:setSecure(boolean)

Sets or removes the Secure flag of the cookie.

Sample:

cookie:setSecure(true)

cookie:getMaxAge()

Returns the maximum age of the cookie in seconds.

Sample:

local maxAge = cookie:getMaxAge()

cookie:setMaxAge(maxAgeAsNumber)

Sets the maximum age of the cookie in seconds.

Sample:

cookie:setMaxAge(300)

cookie:setMaxAgeAndExpires(maxAgeAsNumber)

Sets the maximum age of the cookie in seconds and also calculates and sets the corresponding expiry date.

Sample:

cookie:setMaxAgeAndExpires(300)

attributeSet, attributeValue = cookie:getCustomAttribute(name)

Returns a custom attribute of the cookie. Returns two values:

  • attributeSet: true if the attribute is set.
  • attributeValue: The value of the attribute, or nil if the attribute has no value.

Sample:

local sameSiteIsSet, sameSite = cookie:getCustomAttribute("SameSite")

cookie:setCustomAttribute(name, value)

Sets a custom attribute of the cookie. Possible values for value:

  • A string: sets the attribute to that value.
  • true: sets the attribute without a value.
  • false or nil: removes the attribute.

Sample:

cookie:setCustomAttribute("SameSite", "strict")

tostring(cookie)

Returns a string that can be used directly as a Set-Cookie header value. If you modified the cookie using setter methods, you must add the modified cookie back to the Set-Cookie header; otherwise the changes are ignored.

Sample:

response:addHeader("Set-Cookie", tostring(cookie))