Skip to content

Commit 8c16b27

Browse files
committed
Added info abotu adding cookies using Set-Cookie headers
1 parent e671f8e commit 8c16b27

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

adafruit_httpserver/headers.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ def __init__(self, headers: Union[str, Dict[str, str]] = None) -> None:
6363
self.add(key, value)
6464

6565
def add(self, field_name: str, value: str):
66-
"""Adds a header with the given field name and value."""
66+
"""
67+
Adds a header with the given field name and value.
68+
Allows adding multiple headers with the same name.
69+
"""
6770
self._add_field_value(field_name.lower(), value)
6871

6972
def get(self, field_name: str, default: str = None) -> Union[str, None]:

docs/examples.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,11 @@ You can use cookies to store data on the client side, that will be sent back to
192192
They are often used to store authentication tokens, session IDs, but also to user preferences e.g. theme.
193193

194194
To access cookies, use ``request.cookies`` dictionary.
195-
In order to set cookies, pass ``cookies`` dictionary to ``Response`` constructor.
195+
In order to set cookies, pass ``cookies`` dictionary to ``Response`` constructor or manually add ``Set-Cookie`` header.
196196

197197
.. literalinclude:: ../examples/httpserver_cookies.py
198198
:caption: examples/httpserver_cookies.py
199-
:emphasize-lines: 70,77
199+
:emphasize-lines: 70,74-75,81
200200
:linenos:
201201

202202
Chunked response

examples/httpserver_cookies.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import socketpool
66
import wifi
77

8-
from adafruit_httpserver import Server, Request, Response, GET
8+
from adafruit_httpserver import Server, Request, Response, GET, Headers
99

1010

1111
pool = socketpool.SocketPool(wifi.radio)
@@ -70,6 +70,10 @@ def themed_from_cookie(request: Request):
7070
user_theme = request.cookies.get("theme", "light")
7171
wanted_theme = request.query_params.get("theme", user_theme)
7272

73+
headers = Headers()
74+
headers.add("Set-Cookie", "cookie1=value1")
75+
headers.add("Set-Cookie", "cookie2=value2")
76+
7377
return Response(
7478
request,
7579
themed_template(wanted_theme),

0 commit comments

Comments
 (0)