简体   繁体   English

Python 快速 api 获取内部服务器错误

[英]Python fast api getting internal server error

I am new to python and fastapi, and was playing around it.我是 python 和 fastapi 的新手,正在玩它。

I wrote this code我写了这段代码

from fastapi import FastAPI

app = FastAPI()

people = {
    "1": {
        "name": "abc", 
        "age": 27
    }, 
    "2": {
        "name": "xyz", 
        "age": 60
    }
}

@app.get("/")
def read_root():
    return {"Hello": "World"}


@app.get("/people/")
def get_people(
    min_age: int=0, max_age: int=100
):
    results = [person for person in people.values() if person["age"] >= min_age and person["age"] <= max_age]
    return results


@app.get("/people/{person_id}")
def get_person(person_id: int): 
    return people[person_id]

but on calling @app.get("/people/{person_id}") I am getting 500 error with this as traceback但是在调用@app.get("/people/{person_id}")时,我收到 500 错误作为回溯

Traceback (most recent call last):
  File "/Users/abcB/Desktop/fastapi-tutorial/env/lib/python3.9/site-packages/uvicorn/protocols/http/h11_impl.py", line 407, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
  File "/Users/abcB/Desktop/fastapi-tutorial/env/lib/python3.9/site-packages/uvicorn/middleware/proxy_headers.py", line 78, in __call__
    return await self.app(scope, receive, send)
  File "/Users/abcB/Desktop/fastapi-tutorial/env/lib/python3.9/site-packages/fastapi/applications.py", line 270, in __call__
    await super().__call__(scope, receive, send)
  File "/Users/abcB/Desktop/fastapi-tutorial/env/lib/python3.9/site-packages/starlette/applications.py", line 124, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/Users/abcB/Desktop/fastapi-tutorial/env/lib/python3.9/site-packages/starlette/middleware/errors.py", line 184, in __call__
    raise exc
  File "/Users/abcB/Desktop/fastapi-tutorial/env/lib/python3.9/site-packages/starlette/middleware/errors.py", line 162, in __call__
    await self.app(scope, receive, _send)
  File "/Users/abcB/Desktop/fastapi-tutorial/env/lib/python3.9/site-packages/starlette/middleware/exceptions.py", line 79, in __call__
    raise exc
  File "/Users/abcB/Desktop/fastapi-tutorial/env/lib/python3.9/site-packages/starlette/middleware/exceptions.py", line 68, in __call__
    await self.app(scope, receive, sender)
  File "/Users/abcB/Desktop/fastapi-tutorial/env/lib/python3.9/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in __call__
    raise e
  File "/Users/abcB/Desktop/fastapi-tutorial/env/lib/python3.9/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__
    await self.app(scope, receive, send)
  File "/Users/abcB/Desktop/fastapi-tutorial/env/lib/python3.9/site-packages/starlette/routing.py", line 706, in __call__
    await route.handle(scope, receive, send)
  File "/Users/abcB/Desktop/fastapi-tutorial/env/lib/python3.9/site-packages/starlette/routing.py", line 276, in handle
    await self.app(scope, receive, send)
  File "/Users/abcB/Desktop/fastapi-tutorial/env/lib/python3.9/site-packages/starlette/routing.py", line 66, in app
    response = await func(request)
  File "/Users/abcB/Desktop/fastapi-tutorial/env/lib/python3.9/site-packages/fastapi/routing.py", line 235, in app
    raw_response = await run_endpoint_function(
  File "/Users/abcB/Desktop/fastapi-tutorial/env/lib/python3.9/site-packages/fastapi/routing.py", line 163, in run_endpoint_function
    return await run_in_threadpool(dependant.call, **values)
  File "/Users/abcB/Desktop/fastapi-tutorial/env/lib/python3.9/site-packages/starlette/concurrency.py", line 41, in run_in_threadpool
    return await anyio.to_thread.run_sync(func, *args)
  File "/Users/abcB/Desktop/fastapi-tutorial/env/lib/python3.9/site-packages/anyio/to_thread.py", line 31, in run_sync
    return await get_asynclib().run_sync_in_worker_thread(
  File "/Users/abcB/Desktop/fastapi-tutorial/env/lib/python3.9/site-packages/anyio/_backends/_asyncio.py", line 937, in run_sync_in_worker_thread
    return await future
  File "/Users/abcB/Desktop/fastapi-tutorial/env/lib/python3.9/site-packages/anyio/_backends/_asyncio.py", line 867, in run
    result = context.run(func, *args)
  File "/Users/abcB/Desktop/fastapi-tutorial/./main.py", line 31, in get_person
    return people[person_id]

Can someone point me on what I am doing wrong here?有人可以指出我在这里做错了什么吗?

In your dictionary "people" you have declared the "id" (the keys) as strings.在您的字典“people”中,您已将“id”(键)声明为字符串。 However in the path operation of @app.get("/people/{person_id}") you have declared the person_id as an int.但是在 @app.get("/people/{person_id}") 的路径操作中,您已将 person_id 声明为 int。 That's why the error occurs.这就是发生错误的原因。 Remember that pydantic uses these type declarations for data validation.请记住,pydantic 使用这些类型声明来进行数据验证。

The correct thing would then be:那么正确的事情是:

@app.get("/people/{person_id}")
def get_person(person_id: str): 
    return people[person_id]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 让python工作,内部服务器错误 - Getting python to work, Internal Server Error Python CGI收到500内部服务器错误 - Python CGI getting 500 Internal Server Error 对Yelp API进行身份验证时,我在本地Python服务器上收到内部服务器错误 - I am getting an internal server error on my local Python server when authenticating to Yelp API 500内部服务器错误:Python Bottle API - 500 Internal Server Error: Python Bottle API 为什么通过python请求调用rest api时出现500 Internal Server Error? - Why am I getting 500 Internal Server Error when calling post rest api via python request? Python内部服务器错误 - Python Internal Server Error Apache上的Python flask和fast-cgi-500内部服务器错误(脚本头过早结束) - Python flask & fast-cgi on apache - 500 Internal Server Error (Premature end of script headers) 在python中使用fileConfig时出现500个内部服务器错误 - Getting 500 internal server error while using fileConfig in python Python Flask:未发生API调用,导致500个内部服务器错误 - Python Flask: API call not happening, resulting in 500 internal server error Python API sentinelsat HTTP 状态 500 内部服务器错误: - Python API sentinelsat HTTP status 500 Internal Server Error:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM