How do I set multiple cookies with CherryPy
Asked Answered
S

1

6

From CherryPy documentation, there seems to be only one cookie slot. Here's my example code

def sendCookie(self):
    cookie = cherrypy.response.cookie
    cookie['name'] = 'Chips Ahoy!'
    return 'Cookie is now in your hands.'
sendCookie.exposed = True

I want to set multiple cookies. I'm thinking along these lines, but of course this will just overwrite the first setting.

def sendCookie(self):
    cookie = cherrypy.response.cookie
    cookie2 = cherrypy.response.cookie
    cookie['name'] = 'Chips Ahoy!'
    cookie2['name'] = 'Chocolate Chips'
    return 'Cookie is now in your hands.'
sendCookie.exposed = True

How do I set multiple cookies with CherryPy?

Sidell answered 27/1, 2012 at 13:35 Comment(0)
O
6

I think the first key in cookie should correspond to the name of the cookie, where additional keys would correspond to attributes of that cookie. Thus, instead of using 'name' as the key for your cookies, you should use some unique name.

def sendCookie(self):
    cookies = cherrypy.response.cookie

    cookies['cookie1'] = 'Chips Ahoy!'
    cookies['cookie1']['path'] = '/the/red/bag/'
    cookies['cookie1']['comment'] = 'round'

    cookies['cookie2'] = 'Chocolate Chips'
    cookies['cookie2']['path'] = '/the/yellow/bag/'
    cookies['cookie2']['comment'] = 'thousands'

    return 'Cookies are now in your hands.'
setCookie.exposed = True

Does that work?

Edit: Oops, each morsel has a predefined set of properties, where I was defining my own ('shape' and 'count'). Should be fixed now.

Objective answered 27/1, 2012 at 16:15 Comment(3)
No, only one cookie gets set. In this case, only cookie2. cookie1 is nowhere to be found.Sidell
Oops sorry. It's all good. I was testing on a non-secure http and I set my first cookie to be secure :)Sidell
Hmm, OK. According to this page you need to send 'max-age' or 'expires' for each cookie as well. If that doesn't work, I'm afraid I'm out of ideas. :/Objective

© 2022 - 2024 — McMap. All rights reserved.