章工运维 章工运维
首页
  • linux
  • windows
  • 中间件
  • 监控
  • 网络
  • 存储
  • 安全
  • 防火墙
  • 数据库
  • 系统
  • docker
  • 运维工具
  • other
  • elk
  • K8S
  • ansible
  • Jenkins
  • GitLabCI_CD
  • 随笔
  • 面试
  • 工具
  • 收藏夹
  • Shell
  • python
  • golang
友链
  • 索引

    • 分类
    • 标签
    • 归档
    • 首页 (opens new window)
    • 关于我 (opens new window)
    • 图床 (opens new window)
    • 评论 (opens new window)
    • 导航栏 (opens new window)
周刊
GitHub (opens new window)

章工运维

业精于勤,荒于嬉
首页
  • linux
  • windows
  • 中间件
  • 监控
  • 网络
  • 存储
  • 安全
  • 防火墙
  • 数据库
  • 系统
  • docker
  • 运维工具
  • other
  • elk
  • K8S
  • ansible
  • Jenkins
  • GitLabCI_CD
  • 随笔
  • 面试
  • 工具
  • 收藏夹
  • Shell
  • python
  • golang
友链
  • 索引

    • 分类
    • 标签
    • 归档
    • 首页 (opens new window)
    • 关于我 (opens new window)
    • 图床 (opens new window)
    • 评论 (opens new window)
    • 导航栏 (opens new window)
周刊
GitHub (opens new window)
  • python

    • python基础

    • FastAPI

      • FastAPI-快速入门1
      • FastAPI-路由2
      • FastApi-参数提交3
      • FastApi-响应报文
        • 关于响应状态码status_code
      • FastApi-错误处理
      • FastApi-中间件
      • FastApi-跨域处理
      • 依赖注入之Depends
    • python每日练习脚本

    • python3给防火墙添加放行
    • python生成部署脚本
    • python将多个文件内容输出到一个文件中
    • 使用 Aligo 定时备份服务器文件
    • python监控日志文件并发送钉钉告警
    • python监控数据库脚本并发送钉钉告警
    • 使用python编写自动化发布脚本
    • 查询redis列表某个元素
    • centos7安装python3
    • python环境管理工具介绍
    • conda安装和镜像源配置
    • pip更换国内源
    • python爬虫
    • python环境启动服务报错缺少glibc库版本
    • 监控目录或文件变化
    • 批量更改文件
    • python引用数据库
  • shell

  • go

  • 编程
  • python
  • FastAPI
章工运维
2024-12-04
目录

FastApi-响应报文

# 一、概述

# 使用response_model定义

请求一个接口返回来我们客户端可见的东西都是所谓的响应报文,如响应头,响应码,响应内容等。

通常不会那么傻的用户输入什么就返回什么。以下的官网示例纯粹的演示看:

import uvicorn

from fastapi import FastAPI
from pydantic import BaseModel, EmailStr

app = FastAPI()

class UserIn(BaseModel):
    username: str
    password: str
    email: str
    full_name: str = None

class UserOut(BaseModel):
    username: str
    email: str
    full_name: str = None

@app.post("/user/", response_model=UserOut)
async def create_user(*, user: UserIn):
    return user

if __name__ == '__main__':
    uvicorn.run(app='main:app', host="127.0.0.1", port=8000, reload=True, debug=True)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

json参数

{
    "username":"xiao",
    "password":"1234",
    "email":"12345678@qq.com",
    "full_name":"肖"
}
1
2
3
4
5
6

3d9534de656ffd0b.png

通常再定义我们的API返回响应的时候,一般是返回固定JSON格式的,所以可以直接使用定义response_model为一个字典:

import uvicorn

from fastapi import FastAPI
from typing import Dict

app = FastAPI()

@app.get("/keyword-weights/", response_model=Dict[str, float])
async def read_keyword_weights():
    return {"foo": 2.3, "bar": 3.4}

if __name__ == '__main__':
    uvicorn.run(app='main:app', host="127.0.0.1", port=8000, reload=True, debug=True)
1
2
3
4
5
6
7
8
9
10
11
12
13

json参数

{
    "username":"xiao",
    "password":"1234",
    "email":"12345678@qq.com",
    "full_name":"肖"
}
1
2
3
4
5
6

7e0ad6c4b3b859fa.png

# 关于响应状态码status_code

通常的一个接口请求完成,如果没有什么异常通常会返回200: 如日志打印出来一样:

INFO:     127.0.0.1:58141 - "POST /user/ HTTP/1.1" 400 INFO:     127.0.0.1:58315 - "POST /user/ HTTP/1.1" 200

FastAPI运行我们的指定返回的status_code,如下示例:

import uvicorn

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class UserIn(BaseModel):
    username: str
    password: str
    email: str
    full_name: str = None

class UserOut(BaseModel):
    username: str
    email: str
    full_name: str = None

@app.post("/user/", response_model=UserOut,status_code=500)
async def create_user(*, user: UserIn):
    return user

if __name__ == '__main__':
    uvicorn.run(app='main:app', host="127.0.0.1", port=8000, reload=True, debug=True)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

25dc13ca22d40896.png

由于在路由中定死了状态码500,所以这里就是500

甚至还可以通过导入status来指定:

import uvicorn
from fastapi import FastAPI, status

app = FastAPI()

@app.post("/items/", status_code=status.HTTP_201_CREATED)
async def create_item(name: str):
    return {"name": name}

if __name__ == '__main__':
    uvicorn.run(app='main:app', host="127.0.0.1", port=8000, reload=True, debug=True)
1
2
3
4
5
6
7
8
9
10
11

访问

http://127.0.0.1:8000/items?name=xiao
1

2fa756331c8a3f6d.png

可以看到状态码为201


原文链接 (opens new window)

微信 支付宝
上次更新: 2024/12/13, 12:36:19

← FastApi-参数提交3 FastApi-错误处理→

最近更新
01
shell脚本模块集合
05-13
02
生活小技巧(认知版)
04-29
03
生活小技巧(防骗版)
04-29
更多文章>
Theme by Vdoing | Copyright © 2019-2025 | 点击查看十年之约 | 鄂ICP备2024072800号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式