Forum Discussion

luk's avatar
luk
Boss
7 years ago

Setting cookie attributes (expiration/domain) in page init

I'm able to create a new cookie on page init with

${http.response.addCookie(http.request.createCookie("name", "value"))}

while this does NOT work (but is also stated in the docs, slightly confusing =D):

${http.response.setCookie(http.request.createCookie("name","value"))}

SuzieH see section "http.request.createCookie("name", "value")" and there the example (which is the above code), this does not work, it needs to be addCookie(), not setCookie().

The cookie that is created has some attributes, mainly an expiration date and a domain to which it is scoped, I could not find anything in the docs or community that would tell me how to configure the cookie's attributes, SuzieH could you dig into your internal docs (or dev brains) and see if that is even possible? E.g. something like:

http.request.createCookie("name","value").setExpiration(<maxage in seconds>).setDomain('*.somedomain.tld')

I did try to use the methods for a java servlet httpCookie from: http://docs.oracle.com/javaee/6/api/javax/servlet/http/Cookie.html but with no success, the cookie is not created anymore (can't debug it and do not get any errors because page init...) when I do things like:

<#assign cookie = http.request.createCookie("name", "value") />
<#assign cookie = cookie.setMaxAge(3600) />
${http.response.addCookie(cookie)}

// note, it does not make a difference if everything is first assigned to a variable or not...e.g. this doesn't work either =):

${http.response.addCookie(http.request.createCookie("name", "value").setMaxAge(3600))}

// tried .setHttpOnly(<boolean>) and .setValue() as well, same result: no cookie is created anymore

thanks for clarifying!

  • I was close =)... setting the cookie attributes seems to only work when directly interpolating the variable... that means it has to be written in this way:

    // THIS WILL NOT WORK
    
    <#assign cookie = http.request.createCookie("name", "value") />
    <#assign cookie = cookie.setValue("some manually set value") />
    <#assign cookie = cookie.setHttpOnly(true) />
    // ... etc
    ${http.response.addCookie(cookie)}
    
    // WHILE THIS WILL WORK
    
    <#assign cookie = http.request.createCookie("name", "value") />
    ${cookie.setMaxAge(24*60*60)} // 24 hours
    ${cookie.setDomain(".domain.tld")}
    ${cookie.setPath("/")}
    ${cookie.setValue("some manually set value")}
    ${cookie.setHttpOnly(true)}
    ${cookie.setSecure(true)}
    ${cookie.setComment("Describes what this cookie does")}
    ${http.response.addCookie(cookie)}

    SuzieH I think that should be added to the docs, might save somebody a few hours of research in the future =)

4 Replies

  • I was close =)... setting the cookie attributes seems to only work when directly interpolating the variable... that means it has to be written in this way:

    // THIS WILL NOT WORK
    
    <#assign cookie = http.request.createCookie("name", "value") />
    <#assign cookie = cookie.setValue("some manually set value") />
    <#assign cookie = cookie.setHttpOnly(true) />
    // ... etc
    ${http.response.addCookie(cookie)}
    
    // WHILE THIS WILL WORK
    
    <#assign cookie = http.request.createCookie("name", "value") />
    ${cookie.setMaxAge(24*60*60)} // 24 hours
    ${cookie.setDomain(".domain.tld")}
    ${cookie.setPath("/")}
    ${cookie.setValue("some manually set value")}
    ${cookie.setHttpOnly(true)}
    ${cookie.setSecure(true)}
    ${cookie.setComment("Describes what this cookie does")}
    ${http.response.addCookie(cookie)}

    SuzieH I think that should be added to the docs, might save somebody a few hours of research in the future =)

  • SuzieH's avatar
    SuzieH
    Khoros Alumni (Retired)
    7 years ago

    Thanks, as always, luk. I'm going to run this by the team to be sure we've got all the details correct. Hope to get this updated in the documentation in the next week.

  • SuzieH's avatar
    SuzieH
    Khoros Alumni (Retired)
    7 years ago

    luk I wanted to give you a quick update. Thanks to this post, we're going to be wrapping the java servlet httpCookie in our own context object. Stay tuned to the 18.10 release notes for the details. The ticket is in progress. 

  • Hi luk ,

    thanks for this information. I was exactly looking for this and you saved my day!

     

    SuzieH 

    There is still no info about how to set cookie metadata in the documentation. It would be awesome if somebody could add this part.
    I left a suggestion in the docs, while not beeing completely sure if this fits to addCookie or createCookie.