以下是一个基于FastAPI的简单API示例:

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
async def root():
    return {"message": "Hello World"}

@app.get("/items/{item_id}")
async def read_item(item_id: int, q: str = None):
    return {"item_id": item_id, "q": q}

这个API有两个endpoint:

  • / 返回一个JSON:{"message": "Hello World"}
  • /items/{item_id},其中item_id是一个整数,q是一个可选的字符串参数,返回JSON:{"item_id": item_id, "q": q}

举个例子,如果你在本地启动这个API,可以使用以下URL进行访问:

  • http://localhost:8000/ 返回{"message": "Hello World"}
  • http://localhost:8000/items/42?q=test 返回{"item_id": 42, "q": "test"}
用fastapi写一个api并且给出请求链接实例

原文地址: http://www.cveoy.top/t/topic/f9a 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录