create Web Service with FastAPI and uvicorn

  1. pip install fastapi
  2. pip install uvicorn
    code main.py:
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class People(BaseModel):
    name: str
    age: int
    address: str = None
@app.post('/insert')
def insert(people: People):
    age_after_10_years = people.age + 10
    msg = f'Name: {people.name},age after ten years:{age_after_10_years}'
    return {'success': True, 'msg': msg}
@app.get('/')
def index():
    return {'message': 'Hi, this is your FastApi service!'}

run command: uvicorn main:app --reload, then you can access docs
http://127.0.0.1:8000/docs, you can test POST/GET as below or test it with Postman.

Leave a Reply

Your email address will not be published. Required fields are marked *