Skip to content

Commit 6d7ecd3

Browse files
committed
Updated docs and examples about FormData
1 parent 6a60edc commit 6d7ecd3

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

docs/examples.rst

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,34 @@ Tested on ESP32-S2 Feather.
146146
:emphasize-lines: 25-27,39,51,60,66
147147
:linenos:
148148

149+
Form data parsing
150+
---------------------
151+
152+
Another way to pass data to the handler function is to use form data.
153+
Remember that it is only possible to use it with ``POST`` method.
154+
`More about POST method. <https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST>`_
155+
156+
It is important to use correct ``enctype``, depending on the type of data you want to send.
157+
158+
- ``application/x-www-form-urlencoded`` - For sending simple text data without any special characters including spaces.
159+
If you use it, values will be automatically parsed as strings, but special characters will be URL encoded
160+
e.g. ``"Hello World! ^-$%"`` will be saved as ``"Hello+World%21+%5E-%24%25"``
161+
- ``multipart/form-data`` - For sending text and binary files and/or text data with special characters
162+
When used, values will **not** be automatically parsed as strings, they will stay as bytes instead.
163+
e.g. ``"Hello World! ^-$%"`` will be saved as ``b'Hello World! ^-$%'``, which can be decoded using ``.decode()`` method.
164+
- ``text/plain`` - For sending text data with special characters.
165+
If used, values will be automatically parsed as strings, including special characters, emojis etc.
166+
e.g. ``"Hello World! ^-$%"`` will be saved as ``"Hello World! ^-$%"``, this is the **recommended** option.
167+
168+
If you pass multiple values with the same name, they will be saved as a list, that can be accessed using ``request.data.get_list()``.
169+
Even if there is only one value, it will still get a list, and if there multiple values, but you use ``request.data.get()`` it will
170+
return only the first one.
171+
172+
.. literalinclude:: ../examples/httpserver_form_data.py
173+
:caption: examples/httpserver_form_data.py
174+
:emphasize-lines: 32,47,50
175+
:linenos:
176+
149177
Chunked response
150178
----------------
151179

examples/httpserver_form_data.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# SPDX-FileCopyrightText: 2022 Dan Halbert for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
5+
import socketpool
6+
import wifi
7+
8+
from adafruit_httpserver import Server, Request, Response, GET, POST
9+
10+
11+
pool = socketpool.SocketPool(wifi.radio)
12+
server = Server(pool, debug=True)
13+
14+
15+
FORM_HTML_TEMPLATE = """
16+
<html lang="en">
17+
<head>
18+
<title>Form with {enctype} enctype</title>
19+
</head>
20+
<body>
21+
<a href="/form?enctype=application/x-www-form-urlencoded">
22+
<button>Load <strong>application/x-www-form-urlencoded</strong> form</button>
23+
</a><br />
24+
<a href="/form?enctype=multipart/form-data">
25+
<button>Load <strong>multipart/form-data</strong> form</button>
26+
</a><br />
27+
<a href="/form?enctype=text/plain">
28+
<button>Load <strong>text/plain</strong> form</button>
29+
</a><br />
30+
31+
<h2>Form with {enctype} enctype</h2>
32+
<form action="/form" method="post" enctype="{enctype}">
33+
<input type="text" name="something" placeholder="Type something...">
34+
<input type="submit" value="Submit">
35+
</form>
36+
{submitted_value}
37+
</body>
38+
</html>
39+
"""
40+
41+
42+
@server.route("/form", [GET, POST])
43+
def form(request: Request):
44+
"""
45+
Serve a form with the given enctype, and display back the submitted value.
46+
"""
47+
enctype = request.query_params.get("enctype", "text/plain")
48+
49+
if request.method == POST:
50+
posted_value = request.data.get("something")
51+
52+
return Response(
53+
request,
54+
FORM_HTML_TEMPLATE.format(
55+
enctype=enctype,
56+
submitted_value=(
57+
f"<h3>Submitted form value: {posted_value}</h3>"
58+
if request.method == POST
59+
else ""
60+
),
61+
),
62+
content_type="text/html",
63+
)
64+
65+
66+
server.serve_forever(str(wifi.radio.ipv4_address))

examples/httpserver_neopixel.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ def change_neopixel_color_handler_post_body(request: Request):
3737

3838
data = request.body # e.g b"255,0,0"
3939
r, g, b = data.decode().split(",") # ["255", "0", "0"]
40+
# or
41+
data = request.data # e.g. r=255&g=0&b=0 or r=255\r\nb=0\r\ng=0
42+
r, g, b = data.get("r", 0), data.get("g", 0), data.get("b", 0)
4043

4144
pixel.fill((int(r), int(g), int(b)))
4245

0 commit comments

Comments
 (0)