# coding: utf8
from fastapi import FastAPI
from pydantic import BaseModel
from typing import Optional
app = FastAPI()
class UserIn(BaseModel):
name: str
age: int
desc: Optional[str] = None # 可选
@app.post('/users/')
def create_item(user: UserIn):
return user
# 启动: uvicorn main:app --reload
curl -H "Content-Type: application/json" \
-X POST \
-d '{"name": "tom", "age": 18}' \
127.0.0.1:8000/users/
{"name":"tom","age":18,"desc":null}