bottlepy / bottle

bottle.py is a fast and simple micro-framework for python web-applications.
http://bottlepy.org/
MIT License
8.33k stars 1.46k forks source link

Is it possible to set multiple cookies in HTTPResponse? #1357

Closed mrdc closed 2 years ago

mrdc commented 2 years ago

Hello!

I'm trying to set multiple cookies in HTTPResponse and it looks like it's not possible. This code returns the last cookie (which is expected as it's dict, so keys should be unique):

return HTTPResponse(status = 200, body='abc',headers={'Set-Cookie':'username=Test', 'Set-Cookie':'logged=true'}

Manually setting headers in response works fine, but without using HTTPResponse, because it ignores any manually set headers.

response.set_cookie('logged', 'true')
response.add_header('Set-Cookie', 'username=Test')
avelino commented 2 years ago
{'Set-Cookie':'username=Test', 'Set-Cookie':'logged=true'}

The declared header is a dictionary dict, the behavior of a dictionary is to have unique keys, the way you do this is overwriting the Set-Cookie index.

If you use the response.set_cookie function to declare multiple cookies

defnull commented 2 years ago

The headers parameter also accepts a list of key/value pairs instead of a dict if you need multiple values for the same header.