Xplora
Xplora

Reputation: 905

How to access key-value pair of Cookies in Grails?

For Example:

Cookie name is "example" and key-value pairs stored inside this cookie are: { a: "A", b: "B", c: "C"}

In other words:

Cookie Name: example Cookie Value: {a: "A", b: "B", c: "C"}

def element=cookie(name:'example')
println element.b

Here, element.b doesn't work. Ideally I'm expecting the output as: "B"

Is there any way to access key-value pair of cookies?

I need to access key-value pairs inside a Grails Controller, any alternative way to do this would be appreciated as well!

Upvotes: 0

Views: 337

Answers (1)

injecteer
injecteer

Reputation: 20699

store the data in JSON-format as coockie's value. Thus you can always marshall/unmarshall the values back and forth

UPDATE:

more like that:

writing:

String result = '{ "person":{"name":"xplore","age":3,"pets":["dog","cat"]} }' 
cookieService.setCookie 'cookie_name', result

reading:

def data = new JsonSlurper().parseText cookieService.getCookie('cookie_name')
assert 3 == data.person.age

Upvotes: 1

Related Questions