将 JSON 数据从 JavaScript 前端发送到 FastAPI 后端
在 FastAPI 中,在函数中声明但不属于路径参数的参数会自动解释为查询参数。这种解释与在请求正文中传递 JSON 数据的常见做法不同。
要解决此差异,有多种选项:
使用 Pydantic 模型
创建 Pydantic模型来定义预期的 JSON 正文:
from pydantic import BaseModel class Item(BaseModel): eth_addr: str @app.post("/ethAddress") def add_eth_addr(item: Item): return item
JavaScript Fetch API:
headers: { Accept: "application/json", "Content-Type": "application/json", }, body: JSON.stringify({ eth_addr: "some addr" }),
使用 Body 参数
使用 Body= 和类型:
from fastapi import Body @app.post("/ethAddress") def add_eth_addr(eth_addr: str = Body()): return {"eth_addr": eth_addr}
JavaScript Fetch API:
headers: { Accept: "application/json", "Content-Type": "application/json", }, body: JSON.stringify("some addr"),
使用正文嵌入参数
使用 embed=True 简化仅正文参数:
@app.post("/ethAddress") def add_eth_addr(eth_addr: str = Body(embed=True)): return {"eth_addr": eth_addr}
JavaScript Fetch API :
headers: { Accept: "application/json", "Content-Type": "application/json", }, body: JSON.stringify({ eth_addr: "some addr" }),
有关 JavaScript 的更多详细信息和示例,请参阅:
- https://fastapi.tiangolo.com/advanced/requestBody/
- https://fastapi.tiangolo.com/tutorial/body-forms/#formencoded-or-raw-body-few-parameters
- https://stackoverflow.com/questions/55333220/fastapi-validation- for-payload-when-using-body
请记住,使用查询参数并不是 Web API 中 JSON 正文数据的典型方法。相应地调整您的代码以匹配这些选项。
以上是如何将 JSON 数据从 JavaScript 前端发送到 FastAPI 后端?的详细内容。更多信息请关注PHP中文网其他相关文章!

Tomergelistsinpython,YouCanusethe操作员,estextMethod,ListComprehension,Oritertools

在Python3中,可以通过多种方法连接两个列表:1)使用 运算符,适用于小列表,但对大列表效率低;2)使用extend方法,适用于大列表,内存效率高,但会修改原列表;3)使用*运算符,适用于合并多个列表,不修改原列表;4)使用itertools.chain,适用于大数据集,内存效率高。

使用join()方法是Python中从列表连接字符串最有效的方法。1)使用join()方法高效且易读。2)循环使用 运算符对大列表效率低。3)列表推导式与join()结合适用于需要转换的场景。4)reduce()方法适用于其他类型归约,但对字符串连接效率低。完整句子结束。

pythonexecutionistheprocessoftransformingpypythoncodeintoExecutablestructions.1)InternterPreterReadSthecode,ConvertingTingitIntObyTecode,whepythonvirtualmachine(pvm)theglobalinterpreterpreterpreterpreterlock(gil)the thepythonvirtualmachine(pvm)

Python的关键特性包括:1.语法简洁易懂,适合初学者;2.动态类型系统,提高开发速度;3.丰富的标准库,支持多种任务;4.强大的社区和生态系统,提供广泛支持;5.解释性,适合脚本和快速原型开发;6.多范式支持,适用于各种编程风格。

Python是解释型语言,但也包含编译过程。1)Python代码先编译成字节码。2)字节码由Python虚拟机解释执行。3)这种混合机制使Python既灵活又高效,但执行速度不如完全编译型语言。

useeAforloopWheniteratingOveraseQuenceOrforAspecificnumberoftimes; useAwhiLeLoopWhenconTinuingUntilAcIntiment.ForloopSareIdeAlforkNownsences,而WhileLeleLeleLeleLoopSituationSituationSituationsItuationSuationSituationswithUndEtermentersitations。

pythonloopscanleadtoerrorslikeinfiniteloops,modifyingListsDuringteritation,逐个偏置,零indexingissues,andnestedloopineflinefficiencies


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

Atom编辑器mac版下载
最流行的的开源编辑器

记事本++7.3.1
好用且免费的代码编辑器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具