fastapi

FastAPI: how to read body as any valid json?

◇◆丶佛笑我妖孽 提交于 2021-02-18 14:00:47
问题 Sorry, not proficient in Python. I haven't found the docs for that use case. How can I get the request body, ensure its a valid Json ( any valid json , including numbers, string, booleans and nulls, not only objects and arrays) and get the actual Json. Using pydantic forces the Json to have a specific structure. 回答1: You can find nearly everything inside the Request object You are able to get request body with request.body() from fastapi import Request, FastAPI @app.post("/dummypath") async

FastAPI: how to read body as any valid json?

孤街醉人 提交于 2021-02-18 14:00:34
问题 Sorry, not proficient in Python. I haven't found the docs for that use case. How can I get the request body, ensure its a valid Json ( any valid json , including numbers, string, booleans and nulls, not only objects and arrays) and get the actual Json. Using pydantic forces the Json to have a specific structure. 回答1: You can find nearly everything inside the Request object You are able to get request body with request.body() from fastapi import Request, FastAPI @app.post("/dummypath") async

How to send a progress of operation in a FastAPI app?

…衆ロ難τιáo~ 提交于 2021-02-17 19:14:19
问题 I have deployed a fastapi endpoint, from fastapi import FastAPI, UploadFile from typing import List app = FastAPI() @app.post('/work/test') async def testing(files: List(UploadFile)): for i in files: ....... # do a lot of operations on each file # after than I am just writing that processed data into mysql database # cur.execute(...) # cur.commit() ....... # just returning "OK" to confirm data is written into mysql return {"response" : "OK"} I can request output from the API endpoint and its

Gunicorn is not respecting timeout when using UvicornWorker

橙三吉。 提交于 2021-02-11 17:45:46
问题 I am setting up a timeout check so I made and endpoint: @app.get("/tc", status_code=200) def timeout_check(): time.sleep(500) return "NOT OK" I am using the docker image tiangolo/uvicorn-gunicorn-fastapi:python3.7 and my command to run the server: CMD ["gunicorn","--log-level","debug","--keep-alive","15", "--reload", "-b", "0.0.0.0:8080", "--timeout", "15", "--worker-class=uvicorn.workers.UvicornH11Worker", "--workers=10", "myapp.main:app"] I am expecting the endpoint to fail after 15 seconds

How can I upload multiple files using JavaScript and FastAPI?

两盒软妹~` 提交于 2021-02-11 14:37:56
问题 I followed FastAPI docs and I am trying to send files from my client that wrote in js to my server that wrote in FastAPI. My HTML: <html> <head> <script src="https://code.jquery.com/jquery-2.0.3.js" integrity="sha256-lCf+LfUffUxr81+W0ZFpcU0LQyuZ3Bj0F2DQNCxTgSI=" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script> </head> <body> <input id='fileid' type='file' value="Load miRNA data"/> <input id='fileid2' type='file' value="Load Target

How to update file in CRUD operation using FastApi and MongoDb?

大城市里の小女人 提交于 2021-02-11 12:40:41
问题 For CRUD operation with profile image upload, first I create a pyndantic model in models.py as : For adding a new student class StudentSchema(BaseModel): fullname:str = Field(...) email: EmailStr = Field(...) course_of_study: str = Field(...) year: int = Field(...,gt=0,lt=9) gpa: float = Field(..., le= 4.0) image: Optional[str] And for updating in models.py class UpdateStudentModel(BaseModel): fullname: Optional[str] email: Optional[EmailStr] course_of_study : Optional[str] year : Optional

How to update file in CRUD operation using FastApi and MongoDb?

瘦欲@ 提交于 2021-02-11 12:40:14
问题 For CRUD operation with profile image upload, first I create a pyndantic model in models.py as : For adding a new student class StudentSchema(BaseModel): fullname:str = Field(...) email: EmailStr = Field(...) course_of_study: str = Field(...) year: int = Field(...,gt=0,lt=9) gpa: float = Field(..., le= 4.0) image: Optional[str] And for updating in models.py class UpdateStudentModel(BaseModel): fullname: Optional[str] email: Optional[EmailStr] course_of_study : Optional[str] year : Optional

How to serve static files in FastAPI

别等时光非礼了梦想. 提交于 2021-02-10 20:57:26
问题 I am trying to serve static files that I have in a package_docs directory. When I open in the browzer: http://127.0.0.1:8001/packages/docs/index.html , the page is running. But I want to open the page: http://127.0.0.1:8001/packages/docs/ without the source file. And the output is 404 Not Found app.mount("/packages/docs", StaticFiles(directory=pkg_resources.resource_filename(__name__, 'package_docs') ), name="package_docs") @app.get("/packages/docs/.*", include_in_schema=False) def root():

How to add a custom decorator to a FastAPI route?

拈花ヽ惹草 提交于 2021-02-10 05:21:26
问题 I want to add an auth_required decorator to my endpoints. ( Please consider that this question is about decorators, not middleware ) So a simple decorator looks like this: def auth_required(func): def wrapper(*args, **kwargs): if user_ctx.get() is None: raise HTTPException(...) return func(*args, **kwargs) return wrapper So there are 2 usages: @auth_required @router.post(...) or @router.post(...) @auth_required The first way doesn't work because router.post creates a router that saved into

Python FastAPI Async Variable Sharing

爱⌒轻易说出口 提交于 2021-02-07 10:10:30
问题 If I had the below code, how would the variable service affect the asynchronous nature of the endpoints? Will the variable be shared? Or will it be locked when in use, thus blocking other endpoints from accessing it until the current one is done? I ask the above assuming that Service instances are stateless, i.e. it would be equivalent if I created an instance of Service in each endpoint. I am reluctant to do that because I don't know which is more time consuming, instantiating and destroying