python FastApi quickly do api interface

Don't talk nonsense, just start working!

First, you need to install the modules as follows:

pip install fastapi
pip install uvicorn

Run code

import uvicorn as uvicorn
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()  # 必须实例化该类,启动的时候调用
class People(BaseModel):  # 必须继承
    name: str
    age: int
    address: str
    salary: float

# 请求根目录
@app.get('/')
def index():
    return {
    
    'message': '欢迎来到FastApi 服务!'}

# get请求带参数数据
@app.get('/items/{item_id}')
def items(item_id: int):
    return {
    
    'message': '欢迎来到接口页面'}



# post请求带参数数据
@app.post('/people')
def insert(people: People):
    age = people.age
    msg = f'名字:{people.name},年龄:{age}'
    return {
    
    'success': True, 'msg': msg}
if __name__ == '__main__':
    uvicorn.run(app=app, host="127.0.0.1", port=8080)


Run command

uvicorn main:app --reload

Quick documentation

http://127.0.0.1:8000/docs
http://127.0.0.1:8000/redoc

The get, post request specifically indicates that the post request needs to carry parameters as shown below:

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_37254196/article/details/108143652