adafruit / adafruit/Adafruit_CircuitPython_WSGI
Improve handling for POST parameters
- Dominant language
- Python
- Stars
- 18
- Forks
- 19
- PR merge metrics
- No merged PRs in 30d
Description
I saw this example code for handling POST requests in [another issue](https://github.com/adafruit/Adafruit_CircuitPython_WSGI/issues/11#issuecomment-1004517043):
```py
@web_app.route("/led_on", ["POST"])
def led_on(request):
print("led on!")
r = request.query_params["r"]
g = request.query_params["g"]
b = request.query_params["b"]
status_light.fill((int(r), int(g), int(b)))
return ("200 OK", [], [])
```
It looks pretty straightforward, but when I tried it, it doesn't work. `query_params` only has the parameters from the query string; the POST parameters are in the request body as one might expect.
My working code looks like this:
```py
@web_app.route("/led_on", ["POST"])
def led_on(request):
print("led on!")
if request.method == "POST":
post_params = request.__parse_query_params(request.body.getvalue())
request.body.close()
r = post_params.get('r')
g = post_params.get('g')
b = post_params.get('b')
status_light.fill((int(r), int(g), int(b)))
return ("200 OK", [], [])
```
I can reuse the `__parse_query_params()` logic, but it's a little less intuitive than the first example would suggest. Perhaps this could be improved? Should `query_params` contain request body parameters for POST requests? Maybe it should be a different collection, or only parse the request body on demand.
Contributor guide
No contributing guide indexed for this repository
Research direction
The issue points to request.query_params, request.body, and __parse_query_params; start by reading those request-handling entry points and how POST data is currently exposed. Decide whether body parameters belong in query_params or a separate or on-demand collection, with the existing POST example as the reference case. Done means the chosen behavior is implemented consistently.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- api, backend
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100