Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set multiple cookies with CherryPy

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?

like image 641
Kit Avatar asked Jan 27 '12 13:01

Kit


1 Answers

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.

like image 150
aganders3 Avatar answered Sep 21 '22 17:09

aganders3