官方文档地址:https://fastapi.tiangolo.com/zh/tutorial/request-forms/

接收的不是 JSON,而是表单字段时,要使用 Form

要使用表单,需预先安装 python-multipart:pip install python-multipart

# 从 fastapi 导入 Form
from fastapi import FastAPI, Form app = FastAPI() @app.post("/login/")
async def login(username: str = Form(...), password: str = Form(...)): # 定义 Form 参数
return {"username": username}

OAuth2 规范的 "密码流" 模式规定要通过表单字段发送 username 和 password。该规范要求字段必须命名为 username 和 password,并通过表单字段发送,不能用 JSON。

声明表单体要显式使用 Form ,否则,FastAPI 会把该参数当作查询参数或请求体(JSON)参数。

HTML 表单(<form></form>)向服务器发送数据通常使用「特殊」的编码。FastAPI 要确保从正确的位置读取数据,而不是读取 JSON。

表单数据的「媒体类型」编码一般为 application/x-www-form-urlencoded。但包含文件的表单编码为 multipart/form-data

可在一个路径操作中声明多个 Form 参数,但不能同时声明要接收 JSON 的 Body 字段。因为此时请求体的编码是 application/x-www-form-urlencoded,不是 application/json

代码示例

├── main.py
└── templates
└── index.html
└── post.html

main.py

# -*- coding: UTF-8 -*-

from fastapi import FastAPI, Form, Request
from fastapi.templating import Jinja2Templates app = FastAPI()
templates = Jinja2Templates(directory="templates") @app.post("/user/")
async def form_text(request: Request, username: str = Form(...), password: str = Form(...)):
print('username', username)
print('password', password) # return {'text_1':text_1 , 'text_2': text_2}
return templates.TemplateResponse('index.html', {'request': request, 'username': username, 'password': password}) @app.get("/")
async def main(request: Request):
return templates.TemplateResponse('post.html', {'request': request}) if __name__ == '__main__':
import uvicorn uvicorn.run(app, host="127.0.0.1", port=8000)

templates/index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<meta name="description" content="">
<meta name="author" content="">
<!-- <link rel="icon" href="../../favicon.ico"> --> <title>Signin Template for Bootstrap</title> <!-- Bootstrap core CSS -->
<!-- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> --> <!-- Custom styles for this template -->
<!-- <link href="../static/css/signin.css" rel="stylesheet"> --> </head> <body> <div class="container"> <h1>HELLO..{{ username }}</h1>
<h1>HELLO..{{ password }}</h1>
</div> <!-- /container --> </body>
</html>

templates/post.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body> <div class="container">
<form action="/user/" enctype="application/x-www-form-urlencoded" method="post">
<label>username</label>
<br>
<input name="username" type="username">
<br>
<label>password</label>
<br>
<input name="password" type="password">
<br><br>
<input type="submit">
</form> </div> </body>
</html>

最新文章

  1. 本地项目上传到GitHub
  2. 使用WCF的Trace与Message Log功能
  3. sessionPageState与视图状态存储
  4. WIFI功率修改
  5. Spring(3)—— Junit框架单元测试
  6. centos7 安装及配置
  7. UI优化
  8. css页面重构面试题
  9. Myeclipse快捷键的设置
  10. shell脚本中每次读取文件的一行
  11. 覆写hashCode equal方法
  12. CentOS 7 服务器配置--安装Ftp
  13. WebApi的多版本管理
  14. 忘掉Ghost!利用Win10自带功能,玩转系统备份&amp;恢复 -- 系统重置
  15. redis出现错误:NOAUTH Authentication required.
  16. Hadoop的RPC机制及简单实现
  17. dataway_代码规范
  18. 后端渲染html、前端模板渲染html,jquery的html
  19. 流媒体协议之RTSP详解20170922
  20. hau 1870 愚人节的礼物(栈)

热门文章

  1. 【炼丹Trick】EMA的原理与实现
  2. 从Wannacry到WannaRen:螣龙安科带你深度分析勒索病毒原理
  3. day11 Java反射机制
  4. Harbor企业级私服Docker镜像仓库搭建及应用
  5. HBase学习(四) 二级索引 rowkey设计
  6. CSS(十四):盒子模型
  7. python 上下文管理(with、contextmanager)
  8. 「APIO2010」巡逻 题解
  9. NRooks采样类定义和测试
  10. React重新渲染指南