Skip to main content
Version: 4.6.x.x LTS

Cookie object methods

MethodDescriptionSample
bcx.servlet.http.Cookie.parse(cookie AsInSetCookieHeader)Instantiates a cookie expecting a string with the same syntax like in the "Set-Cookie" header.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.local cookie = bcx.servlet.http.Cookie.new("LuaCookie", "SomeValue")
cookie:getName()Returns the name of the cookie.local name = cookie:getName()
cookie:getValue()Returns the value of the cookie. If the value is quoted, the quotes are returned as well.local value = cookie:getValue()
cookie:setValue(valueAsString)Sets the value of the cookie.cookie:setValue("MyNewValue")
cookie:getComment()Returns the comment of the cookie.local comment = cookie:getComment()
cookie:setComment(commentAsString)Sets the comment of the cookie.cookie:setComment("MyNewComment")
cookie:getDomain()Returns the domain of the cookie.local domain = cookie:getDomain()
cookie:setDomain(domainAsString)Sets the domain of the cookie.cookie:setDomain("MyNewDomain")
cookie:getPath()Returns the path of the cookie.local path = cookie:getPath()
cookie:setPath(pathAsString)Sets the path of the cookie.cookie:setPath("MyNewPath")
cookie:getExpires()Returns the expire date of the cookie.local expires = cookie:getExpires()
cookie:setExpires(expiresAsString)Sets the expire date of the cookie.Important: The system does not verify whether the passed string contains a valid date. To be sure that the date format is correct, use setMaxAgeAndExpires() insteadcookie:setExpires("Thu, 16-Feb-2017 15:45:32 GMT")
cookie:isHttpOnly()Returns the boolean "true" if the HttpOnly flag is set (see the parameter cookie:setHttpOnly(boolean)).local httpOnly = cookie:isHttpOnly()
cookie:setHttpOnly(boolean)Sets or removes the HttpOnly flag of the cookie.cookie:setHttpOnly(true)
cookie:isSecure()Returns the boolean "true" if the Secure flag is set (see the parameter cookie:setSecure(boolean)).local secure = cookie:isSecure()
cookie:setSecure(boolean)Sets or removes the Secure flag of the cookie.cookie:setSecure(true)
cookie:getVersion()Returns the version of the cookie as a number.local version = cookie:getVersion()
cookie:setVersion(versionAsNumber)Sets the version of the cookie.cookie:setVersion(1)
cookie:getMaxAge()Returns the maximum age of the cookie as a number of seconds.local maxAge = cookie:getMaxAge()
cookie:setMaxAge(maxAgeAsNumber)Sets the maximum age of the cookie in seconds.cookie:setMaxAge(300)
cookie:setMaxAgeAndExpires(maxAgeAsNumber)Sets the maximum age of the cookie in seconds. It also calculates and sets the expiry date of the cookie.cookie:setMaxAgeAndExpires(300)
cookie:getCustomAttribute(name)Returns the customAttribute "name" of the cookie (as set in the parameter cookie:setCustomAttribute(name, value)). Two values are returned: attributeSet, attributeValue = cookie:getCustomAttribute(name)These values have the following meaning: *attributeSet: A boolean value, which is "true" if the attribute is set. * attributeValue: The string containing either the value of this attribute or nil, if the attribute has no value.
local sameSiteIsSet, sameSite = cookie:getCustomAttribute("SameSite")
cookie:setCustomAttribute(name, value)Sets the customAttribute of the cookie. The following values are possible:A string: The passed string will be set as the value of the attribute The boolean "true": The field will be set without a value. * The boolean "false" or nil: The value will be removed.
cookie:setCustomAttribute("SameSite", "strict")
tostring(cookie)Returns a string that can be used directly as "Set-Cookie" header. If you change the value of a parsed cookie by using one of the setter methods described above, you have to add the modified cookie to the "Set-Cookie" header. Else, the changes will be ignored.response:addHeader("Set-Cookie", tostring(cookie))