Skip to content

Commit b271a3c

Browse files
committed
Fix: data to form_data in docs and examples
1 parent 5aea936 commit b271a3c

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

docs/examples.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ It is important to use correct ``enctype``, depending on the type of data you wa
165165
If used, values will be automatically parsed as strings, including special characters, emojis etc.
166166
e.g. ``"Hello World! ^-$%"`` will be saved as ``"Hello World! ^-$%"``, this is the **recommended** option.
167167

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
168+
If you pass multiple values with the same name, they will be saved as a list, that can be accessed using ``request.form_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.form_data.get()`` it will
170170
return only the first one.
171171

172172
.. literalinclude:: ../examples/httpserver_form_data.py

examples/httpserver_form_data.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ def form(request: Request):
4747
enctype = request.query_params.get("enctype", "text/plain")
4848

4949
if request.method == POST:
50-
posted_value = request.data.get("something")
50+
posted_value = request.form_data.get("something")
5151

5252
return Response(
5353
request,
5454
FORM_HTML_TEMPLATE.format(
5555
enctype=enctype,
5656
submitted_value=(
57-
f"<h3>Submitted form value: {posted_value}</h3>"
57+
f"<h3>Enctype: {enctype}</h3>\n<h3>Submitted form value: {posted_value}</h3>"
5858
if request.method == POST
5959
else ""
6060
),

examples/httpserver_neopixel.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,23 @@ def change_neopixel_color_handler_query_params(request: Request):
3131
return Response(request, f"Changed NeoPixel to color ({r}, {g}, {b})")
3232

3333

34-
@server.route("/change-neopixel-color", POST)
34+
@server.route("/change-neopixel-color/body", POST)
3535
def change_neopixel_color_handler_post_body(request: Request):
3636
"""Changes the color of the built-in NeoPixel using POST body."""
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
40+
41+
pixel.fill((int(r), int(g), int(b)))
42+
43+
return Response(request, f"Changed NeoPixel to color ({r}, {g}, {b})")
44+
45+
46+
@server.route("/change-neopixel-color/form-data", POST)
47+
def change_neopixel_color_handler_post_form_data(request: Request):
48+
"""Changes the color of the built-in NeoPixel using POST form data."""
49+
50+
data = request.form_data # e.g. r=255&g=0&b=0 or r=255\r\nb=0\r\ng=0
4251
r, g, b = data.get("r", 0), data.get("g", 0), data.get("b", 0)
4352

4453
pixel.fill((int(r), int(g), int(b)))

0 commit comments

Comments
 (0)