Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Sending incorrect data cause 500 error #914

@c2d628ba98

Description

@c2d628ba98

I've got this simple example:

from typing import List

from fastapi import FastAPI, Body


class User(BaseModel):
    name: str


@app.post('/test/')
async def test(users: List[User], test: str = Body(...)):
    return {'users': users, 'test': test}

When sending incorrect payload the server returns 500 error:

# curl -s -D - -o /dev/null -X POST "localhost:8080/test/" -H  "accept: application/json" -H  "Content-Type: application/json" -d "[]"
HTTP/1.1 500 Internal Server Error
date: Fri, 24 Jan 2020 10:59:47 GMT
server: uvicorn
content-length: 1264
content-type: text/plain; charset=utf-8

Server exception:

INFO:     127.0.0.1:60652 - "POST /test/ HTTP/1.1" 500 Internal Server Error
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/test/venv38/lib/python3.8/site-packages/uvicorn/protocols/http/httptools_impl.py", line 385, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "/test/venv38/lib/python3.8/site-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__
    return await self.app(scope, receive, send)
  File "/test/venv38/lib/python3.8/site-packages/fastapi/applications.py", line 140, in __call__
    await super().__call__(scope, receive, send)
  File "/test/venv38/lib/python3.8/site-packages/starlette/applications.py", line 134, in __call__
    await self.error_middleware(scope, receive, send)
  File "/test/venv38/lib/python3.8/site-packages/starlette/middleware/errors.py", line 178, in __call__
    raise exc from None
  File "/test/venv38/lib/python3.8/site-packages/starlette/middleware/errors.py", line 156, in __call__
    await self.app(scope, receive, _send)
  File "/test/venv38/lib/python3.8/site-packages/starlette/exceptions.py", line 73, in __call__
    raise exc from None
  File "/test/venv38/lib/python3.8/site-packages/starlette/exceptions.py", line 62, in __call__
    await self.app(scope, receive, sender)
  File "/test/venv38/lib/python3.8/site-packages/starlette/routing.py", line 590, in __call__
    await route(scope, receive, send)
  File "/test/venv38/lib/python3.8/site-packages/starlette/routing.py", line 208, in __call__
    await self.app(scope, receive, send)
  File "/test/venv38/lib/python3.8/site-packages/starlette/routing.py", line 41, in app
    response = await func(request)
  File "/test/venv38/lib/python3.8/site-packages/fastapi/routing.py", line 115, in app
    solved_result = await solve_dependencies(
  File "/test/venv38/lib/python3.8/site-packages/fastapi/dependencies/utils.py", line 547, in solve_dependencies
    ) = await request_body_to_args(  # body_params checked above
  File "/test/venv38/lib/python3.8/site-packages/fastapi/dependencies/utils.py", line 637, in request_body_to_args
    value = received_body.get(field.alias)
AttributeError: 'list' object has no attribute 'get'

So the body is not validated in this case?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions