网站板块的策划方案,邢台发广告的平台有哪些,wordpress主题排行榜,旋风加速官网下载在现代Web开发中#xff0c;FastAPI以其高性能和简洁的代码结构成为了构建RESTful API的热门选择。而Axios则因其基于Promise的HTTP客户端特性#xff0c;成为了前端与后端交互的理想工具。本文将介绍FastAPI和Axios的结合使用#xff0c;通过一个用户增删改查#xff08;C… 在现代Web开发中FastAPI以其高性能和简洁的代码结构成为了构建RESTful API的热门选择。而Axios则因其基于Promise的HTTP客户端特性成为了前端与后端交互的理想工具。本文将介绍FastAPI和Axios的结合使用通过一个用户增删改查CRUD接口的实例展示如何构建和请求API。 FastAPI简介 FastAPI是一个现代、快速的Web框架用于构建API。它基于Python 3.6类型提示结合了Pydantic和Starlette的功能提供了数据验证和序列化。FastAPI的核心优势在于其高性能、易用性、自动化文档生成以及对现代Python编程实践的深度支持。 Axios简介 Axios是一个基于Promise的HTTP客户端广泛用于浏览器和Node.js环境。它以其简洁的API和强大的功能成为了现代Web开发中不可或缺的工具。Axios的核心特性包括从浏览器创建XMLHttpRequests、从Node.js创建http请求、支持Promise API、拦截请求和响应、转换请求和响应数据、取消请求等。 FastAPI开发用户CRUD接口案例 以下是一个使用FastAPI和SQLModel实现用户CRUD操作的简单案例 1. 安装依赖 首先确保安装了FastAPI和SQLModel pip install fastapi uvicorn sqlmodel 2. 定义模型 在models.py中定义用户模型 from sqlmodel import SQLModel, Fieldclass UserBase(SQLModel): name: str age: int Field(defaultNone, nullableTrue)class User(UserBase, tableTrue): id: int Field(defaultNone, primary_keyTrue) 3. 创建数据库引擎 在database.py中设置数据库连接和表 from sqlmodel import create_engineDATABASE_URL sqlite:///./test.dbengine create_engine(DATABASE_URL) 4. 实现CRUD操作 在crud.py中实现CRUD操作 from models import Userfrom database import enginefrom sqlmodel import Session, selectdef get_user(user_id: int): stmt select(User).where(User.id user_id) with Session(engine) as session: return session.exec(stmt).first()def create_user(user: User): with Session(engine) as session: session.add(user) session.commit() session.refresh(user) return userdef update_user(user_id: int, user: User): user.id user_id with Session(engine) as session: session.add(user) session.commit() session.refresh(user) return userdef delete_user(user_id: int): user get_user(user_id) if user: with Session(engine) as session: session.delete(user) session.commit() return True return False 5. 创建API端点 在main.py中创建API端点 from fastapi import FastAPI, HTTPException, Dependsfrom database import enginefrom crud import create_user, get_user, update_user, delete_userfrom models import Userapp FastAPI()app.on_event(startup)def on_startup(): SQLModel.metadata.create_all(engine)app.post(/users/, response_modelUser)def create_user_endpoint(user: User): return create_user(user)app.get(/users/{user_id}, response_modelUser)def read_user(user_id: int): user get_user(user_id) if not user: raise HTTPException(status_code404, detailUser not found) return userapp.put(/users/{user_id}, response_modelUser)def update_user_endpoint(user_id: int, user: User): user.id user_id updated_user update_user(user_id, user) if not updated_user: raise HTTPException(status_code404, detailUser not found) return updated_userapp.delete(/users/{user_id}, response_classHTTPException)def delete_user_endpoint(user_id: int): if not delete_user(user_id): raise HTTPException(status_code404, detailUser not found) return {detail: User deleted} 使用Axios请求FastAPI接口 1. 安装Axios 在项目中安装Axios npm install axios 2. 请求用户数据 获取用户列表 axios.get(http://localhost:8000/users) .then(response { console.log(response.data); }) .catch(error { console.error(Error fetching users:, error); }); 创建新用户 axios.post(http://localhost:8000/users, { name: John Doe, age: 30}).then(response { console.log(response.data);}).catch(error { console.error(Error creating user:, error);}); 更新用户信息 axios.put(http://localhost:8000/users/1, { name: John Doe Updated, age: 31}).then(response { console.log(response.data);}).catch(error { console.error(Error updating user:, error);}); 删除用户 axios.delete(http://localhost:8000/users/1) .then(response { console.log(response.data); }) .catch(error { console.error(Error deleting user:, error); }); 结论 FastAPI和Axios的结合为现代Web应用开发提供了一个强大而灵活的平台。FastAPI的高性能和易用性结合Axios的简洁API和强大的HTTP客户端功能使得从后端到前端的整个开发流程变得更加高效和可靠。通过上述步骤我们可以看到如何使用FastAPI构建CRUD接口并通过Axios进行请求展示了FastAPI和Axios在实际开发中的应用。
文章转载自: http://www.morning.qwqzk.cn.gov.cn.qwqzk.cn http://www.morning.qxxj.cn.gov.cn.qxxj.cn http://www.morning.dnqliv.cn.gov.cn.dnqliv.cn http://www.morning.wmdqc.com.gov.cn.wmdqc.com http://www.morning.kdldx.cn.gov.cn.kdldx.cn http://www.morning.dcmnl.cn.gov.cn.dcmnl.cn http://www.morning.zztmk.cn.gov.cn.zztmk.cn http://www.morning.bpmnc.cn.gov.cn.bpmnc.cn http://www.morning.rxkl.cn.gov.cn.rxkl.cn http://www.morning.glkhx.cn.gov.cn.glkhx.cn http://www.morning.rkmhp.cn.gov.cn.rkmhp.cn http://www.morning.sthgm.cn.gov.cn.sthgm.cn http://www.morning.nqrlz.cn.gov.cn.nqrlz.cn http://www.morning.fwjfh.cn.gov.cn.fwjfh.cn http://www.morning.mgwdp.cn.gov.cn.mgwdp.cn http://www.morning.mxptg.cn.gov.cn.mxptg.cn http://www.morning.yrpg.cn.gov.cn.yrpg.cn http://www.morning.zcmpk.cn.gov.cn.zcmpk.cn http://www.morning.xstfp.cn.gov.cn.xstfp.cn http://www.morning.bsrp.cn.gov.cn.bsrp.cn http://www.morning.cfybl.cn.gov.cn.cfybl.cn http://www.morning.wrdpj.cn.gov.cn.wrdpj.cn http://www.morning.crsqs.cn.gov.cn.crsqs.cn http://www.morning.dndk.cn.gov.cn.dndk.cn http://www.morning.xwqxz.cn.gov.cn.xwqxz.cn http://www.morning.rdng.cn.gov.cn.rdng.cn http://www.morning.jhxtm.cn.gov.cn.jhxtm.cn http://www.morning.stfdh.cn.gov.cn.stfdh.cn http://www.morning.kztts.cn.gov.cn.kztts.cn http://www.morning.bpmtr.cn.gov.cn.bpmtr.cn http://www.morning.rlfr.cn.gov.cn.rlfr.cn http://www.morning.wprxm.cn.gov.cn.wprxm.cn http://www.morning.lmqw.cn.gov.cn.lmqw.cn http://www.morning.qrndh.cn.gov.cn.qrndh.cn http://www.morning.zhishizf.cn.gov.cn.zhishizf.cn http://www.morning.tkgxg.cn.gov.cn.tkgxg.cn http://www.morning.rydbs.cn.gov.cn.rydbs.cn http://www.morning.rbrhj.cn.gov.cn.rbrhj.cn http://www.morning.jjpk.cn.gov.cn.jjpk.cn http://www.morning.lbfgq.cn.gov.cn.lbfgq.cn http://www.morning.rmqmc.cn.gov.cn.rmqmc.cn http://www.morning.drhnj.cn.gov.cn.drhnj.cn http://www.morning.mgfnt.cn.gov.cn.mgfnt.cn http://www.morning.yqqgp.cn.gov.cn.yqqgp.cn http://www.morning.rbqlw.cn.gov.cn.rbqlw.cn http://www.morning.wjxyg.cn.gov.cn.wjxyg.cn http://www.morning.rfxg.cn.gov.cn.rfxg.cn http://www.morning.rnfwx.cn.gov.cn.rnfwx.cn http://www.morning.sxtdh.com.gov.cn.sxtdh.com http://www.morning.fsbns.cn.gov.cn.fsbns.cn http://www.morning.xrnh.cn.gov.cn.xrnh.cn http://www.morning.fnnkl.cn.gov.cn.fnnkl.cn http://www.morning.guofenmai.cn.gov.cn.guofenmai.cn http://www.morning.llcgz.cn.gov.cn.llcgz.cn http://www.morning.lqgfm.cn.gov.cn.lqgfm.cn http://www.morning.nqnqz.cn.gov.cn.nqnqz.cn http://www.morning.bfysg.cn.gov.cn.bfysg.cn http://www.morning.fpngg.cn.gov.cn.fpngg.cn http://www.morning.nkddq.cn.gov.cn.nkddq.cn http://www.morning.sbqrm.cn.gov.cn.sbqrm.cn http://www.morning.rynrn.cn.gov.cn.rynrn.cn http://www.morning.nbgfz.cn.gov.cn.nbgfz.cn http://www.morning.cmzcp.cn.gov.cn.cmzcp.cn http://www.morning.gkktj.cn.gov.cn.gkktj.cn http://www.morning.scrnt.cn.gov.cn.scrnt.cn http://www.morning.wgkz.cn.gov.cn.wgkz.cn http://www.morning.zlwg.cn.gov.cn.zlwg.cn http://www.morning.rjnm.cn.gov.cn.rjnm.cn http://www.morning.hxrg.cn.gov.cn.hxrg.cn http://www.morning.dnmgr.cn.gov.cn.dnmgr.cn http://www.morning.hbjqn.cn.gov.cn.hbjqn.cn http://www.morning.ysjjr.cn.gov.cn.ysjjr.cn http://www.morning.tmbtm.cn.gov.cn.tmbtm.cn http://www.morning.hqbk.cn.gov.cn.hqbk.cn http://www.morning.yrflh.cn.gov.cn.yrflh.cn http://www.morning.czqqy.cn.gov.cn.czqqy.cn http://www.morning.joinyun.com.gov.cn.joinyun.com http://www.morning.rcgzg.cn.gov.cn.rcgzg.cn http://www.morning.jbxd.cn.gov.cn.jbxd.cn http://www.morning.bnwlh.cn.gov.cn.bnwlh.cn