Skip to navigation
GET, POST and DELETE with FASTAPI
08.07.21
example: import fastapi from api.models import margin from fastapi_utils.cbv import cbv from datetime import datetime from fastapi.responses import StreamingResponse, ORJSONResponse from api.utils import mssql_connect as mcon router = fastapi.APIRouter() @cbv(router) class Av0_material_categories: @router.get("/av0_material_categories", response_class=ORJSONResponse) async def get(self, f: str = 'json'): # http://127.0.0.1:8071//av0_material_categories # http://sapif.salamander-jewelry.net/av0_material_categories self.api = { '_API':"stock", 'source': '', 'col_model': [ {"name": "code","label": "Code", "sorttype": "string"}, {"name": "material_description","label": "Description", "sorttype": "string"}, {"name": "name","label": "Name", "sorttype": "string"}, ], 'data': [], } if f == 'col_model': return self.api['col_model'] elif f== 'data': self.api['data'] = await self.query() return self.api['data'] else: self.api['data'] = await self.query() return self.api @router.post("/av0_material_categories", response_class=ORJSONResponse) async def post(self, request: fastapi.Request): # http://127.0.0.1:8071//av0_material_categories # http://sapif.salamander-jewelry.net/av0_material_categories d = await request.json() sql = """ UPDATE [SL].[dbo].[av0_material_categories] SET [name] = '{0}' ,[material_description] = '{1}' WHERE [code] = '{2}' """.format(d['name'],d['material_description'],d['_id_val']) #print(sql) ret = await mcon.execute_231(sql) #changeserver #print(ret) return {'msg': 'ok'} @router.delete("/av0_material_categories", response_class=ORJSONResponse) async def delete(self, _id: str = False): # http://127.0.0.1:8071/av0_material_categories # http://sapif.salamander-jewelry.net/av0_material_categories sql = """ DELETE FROM [SL].[dbo].[av0_material_categories] WHERE [code] = '{0}' """.format(_id) print(sql) ret = await mcon.execute_231(sql) #changeserver print(ret) return {'msg': 'ok'} async def query(self): sql = """ SELECT code AS id ,code AS code ,material_description ,[name] FROM [SL].[dbo].[av0_material_categories] """ #print(sql) ret = await mcon.connect_231(sql) #changeserver return ret
https://fastapi.tiangolo.com/tutorial/body/
Reply
Anonymous
Information Epoch 1732544777
When in doubt, use brute force.
Home
Notebook
Contact us