mirror of https://github.com/tiangolo/fastapi.git
♻ Assume request bodies contain JSON when no Content-Type header is provided (#3456)
This commit is contained in:
parent
7eb17fc874
commit
edf6b2d61f
|
|
@ -184,7 +184,9 @@ def get_request_handler(
|
||||||
if body_bytes:
|
if body_bytes:
|
||||||
json_body: Any = Undefined
|
json_body: Any = Undefined
|
||||||
content_type_value = request.headers.get("content-type")
|
content_type_value = request.headers.get("content-type")
|
||||||
if content_type_value:
|
if not content_type_value:
|
||||||
|
json_body = await request.json()
|
||||||
|
else:
|
||||||
message = email.message.Message()
|
message = email.message.Message()
|
||||||
message["content-type"] = content_type_value
|
message["content-type"] = content_type_value
|
||||||
if message.get_content_maintype() == "application":
|
if message.get_content_maintype() == "application":
|
||||||
|
|
|
||||||
|
|
@ -229,6 +229,20 @@ def test_geo_json():
|
||||||
assert response.status_code == 200, response.text
|
assert response.status_code == 200, response.text
|
||||||
|
|
||||||
|
|
||||||
|
def test_no_content_type_is_json():
|
||||||
|
response = client.post(
|
||||||
|
"/items/",
|
||||||
|
data='{"name": "Foo", "price": 50.5}',
|
||||||
|
)
|
||||||
|
assert response.status_code == 200, response.text
|
||||||
|
assert response.json() == {
|
||||||
|
"name": "Foo",
|
||||||
|
"description": None,
|
||||||
|
"price": 50.5,
|
||||||
|
"tax": None,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def test_wrong_headers():
|
def test_wrong_headers():
|
||||||
data = '{"name": "Foo", "price": 50.5}'
|
data = '{"name": "Foo", "price": 50.5}'
|
||||||
invalid_dict = {
|
invalid_dict = {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue