Skip to content

Commit ec8b00b

Browse files
committed
Fix for #10
1 parent b2fae4b commit ec8b00b

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

lib/apis/methods.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#
44

55
import json
6+
from urllib.parse import parse_qs
67

78
from fastapi import APIRouter
89
from fastapi import FastAPI, Header, Request
@@ -47,9 +48,17 @@ async def get(request: Request):
4748
async def post(request: Request):
4849

4950
data = data_default
51+
content_type = request.headers.get("Content-Type", "application/x-www-form-urlencoded")
5052

5153
try:
52-
data = await request.json()
54+
if content_type == "application/json":
55+
data = await request.json()
56+
57+
else:
58+
# Assume that it was a normal form submission
59+
body = await request.body()
60+
data = parse_qs(body)
61+
5362
except json.decoder.JSONDecodeError as e:
5463
logger.warning(f"{__name__}:post(): Caught error deserializing JSON: {e}")
5564

lib/fastapi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33
# Store our app version here.
44
#
5-
app_version = "0.0.46"
5+
app_version = "0.0.47"
66

77
tags_metadata = [
88
{

tests/test_methods.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,38 +19,51 @@ def test_get():
1919

2020
def test_post():
2121

22+
headers_json = {"Content-Type": "application/json"}
23+
headers_form = {"Content-Type": "application/x-www-form-urlencoded"}
24+
2225
data = {}
23-
response = client.post("/post", json = json.dumps(data))
26+
response = client.post("/post", json = json.dumps(data), headers = headers_json)
2427
assert response.status_code == 200
2528
assert response.json()["source"]["ip"] == "testclient"
2629
assert response.json()["headers"]["host"] == "testserver"
2730
data = json.loads(response.json()["data"])
2831
assert data == {}
2932

3033
data = "broken json"
31-
response = client.post("/post", json = json.dumps(data))
34+
response = client.post("/post", json = json.dumps(data), headers = headers_json)
3235
assert response.status_code == 200
3336
assert response.json()["source"]["ip"] == "testclient"
3437
assert response.json()["headers"]["host"] == "testserver"
3538
data = json.loads(response.json()["data"])
3639
assert data == "broken json"
3740

3841
data = { "cheetah": "chirp", "goat": "bleat" }
39-
40-
response = client.post("/post", json = json.dumps(data))
42+
response = client.post("/post", json = json.dumps(data), headers = headers_json)
4143
assert response.status_code == 200
4244
assert response.json()["source"]["ip"] == "testclient"
4345
assert response.json()["headers"]["host"] == "testserver"
4446
data = json.loads(response.json()["data"])
4547
assert data["cheetah"] == "chirp"
4648

4749
data = [ data ]
48-
49-
response = client.post("/post", json = json.dumps(data))
50+
response = client.post("/post", json = json.dumps(data), headers = headers_json)
5051
assert response.status_code == 200
5152
data = json.loads(response.json()["data"])
5253
assert data[0]["cheetah"] == "chirp"
5354

55+
#
56+
# Test regular form data
57+
#
58+
form_data = "birthyear=1905&press=%20OK%20"
59+
response = client.post("/post", data = form_data, headers = headers_form)
60+
assert response.status_code == 200
61+
assert response.json()["source"]["ip"] == "testclient"
62+
assert response.json()["headers"]["host"] == "testserver"
63+
data = response.json()["data"]
64+
assert data["birthyear"][0] == "1905"
65+
assert data["press"][0] == " OK "
66+
5467

5568
def test_put():
5669

0 commit comments

Comments
 (0)