Forum Discussion

id-chalmers's avatar
11 years ago

getting client cookies name and value

What is the best way to A: check that a cookie of name "cookie-a" has been set in the client, and B: to get the value and expiration of cookie-a?

I can do this fairly easily with javascript (document.cookie();) and have confirmed that the cookie really is there. 

 

I have used  the documentation here: http://lithosphere.lithium.com/t5/developers-knowledge-base/http-request-FreeMarker-context-object/ta-p/9323 to produce: 

 

<#assign myCookie =http.request.cookies.name.cookie-a />

<#if myCookie.value == "value">

     {do stuff}

<#/if>

 

but it throws an error

also if I print myCookie.name I just get whatever name I used in the request (regardless of whether it exists on the client or not). 

 

Please tell me the correct syntax for getting a cookie's value!

 

Thanks!

 

 

  • Hi,

     

    this should work:

     

    <#assign cookie = http.request.cookies.name.get("cookie-a") />
    <#if cookie?? && cookie.value?? && (cookie.value == "value")>
    todo
    </#if>

     

  • ChiaraS's avatar
    ChiaraS
    Lithium Alumni (Retired)

    Hi,

     

    this should work:

     

    <#assign cookie = http.request.cookies.name.get("cookie-a") />
    <#if cookie?? && cookie.value?? && (cookie.value == "value")>
    todo
    </#if>

     

  • What's the error saying? The problem might be just as simple as the "-" in your cookie name being considered a minus sign. Try using a different cookie name with only alphanumeric characters.