Skip to content

Commit

Permalink
Update docs to include notes about accessing form data.
Browse files Browse the repository at this point in the history
Update unit tests to verify URL form queries work correctly.
  • Loading branch information
patchgamestudio committed Jan 2, 2022
1 parent a37cb2f commit 79f461e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
flake8==4.0.1
black==21.12b0
websockets==10.1
11 changes: 11 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,17 @@ async def json(request):
return jsonify({"hello": "world"})
```

### Getting URL form data
You can access URL form data with the request object similar to how you access params.

```python3
@app.get("/get")
async def query(request):
form_data = request.get("queries", {})
print(form_data)
return jsonify({"queries": form_data})
```

### Returning a JSON Response
You can also serve JSON responses when serving HTTP request using the following way.

Expand Down
5 changes: 5 additions & 0 deletions integration_tests/base_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ async def test(request):
async def json_get():
return jsonify({"hello": "world"})

@app.get("/query")
async def query_get(request):
query_data = request["queries"]
return jsonify(query_data)


@app.post("/jsonify/:id")
async def json(request):
Expand Down
6 changes: 6 additions & 0 deletions integration_tests/test_get_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ def test_html(session):
r = requests.get(f"{BASE_URL}/test/123")
assert "Hello world. How are you?" in r.text

def test_queries(session):
r = requests.get(f"{BASE_URL}/query?hello=robyn")
assert r.json()=={"hello":"robyn"}

r = requests.get(f"{BASE_URL}/query")
assert r.json()=={}

0 comments on commit 79f461e

Please sign in to comment.