How can I get headers or a specific header from my backend API?

It’s pretty similar, you can do from fastapi import FastAPI, Request @app.get(“/”) async def root(request: Request): my_header = request.headers.get(‘header-name’) … NOTE: that it’s lowercased Example: from fastapi import FastAPI, Request app = FastAPI() @app.get(“/”) async def root(request: Request): my_header = request.headers.get(‘my-header’) return {“message”: my_header} Now if you run this app with uvicorn on your localhost, … Read more

Adding python logging to FastApi endpoints, hosted on docker doesn’t display API Endpoints logs

Inspired by JPG’s answer, but using a pydantic model looked cleaner. You might want to expose more variables. This config worked good for me. from pydantic import BaseModel class LogConfig(BaseModel): “””Logging configuration to be set for the server””” LOGGER_NAME: str = “mycoolapp” LOG_FORMAT: str = “%(levelprefix)s | %(asctime)s | %(message)s” LOG_LEVEL: str = “DEBUG” # … Read more

FastAPI runs api-calls in serial instead of parallel fashion

As per FastAPI’s documentation: When you declare a path operation function with normal def instead of async def, it is run in an external threadpool that is then awaited, instead of being called directly (as it would block the server). also, as described here: If you are using a third party library that communicates with … Read more

Query parameters from pydantic model

The documentation gives a shortcut to avoid this kind of repetitions. In this case, it would give: from fastapi import Depends @app.post(“/test-query-params”) def test_query(model: Model = Depends()): pass This will allow you to request /test-query-params?x=1&y=2 and will also produce the correct OpenAPI description for this endpoint. Similar solutions can be used for using Pydantic models … Read more

How to create routes with FastAPI within a class

This can be done by using an APIRouter‘s add_api_route method: from fastapi import FastAPI, APIRouter class Hello: def __init__(self, name: str): self.name = name self.router = APIRouter() self.router.add_api_route(“/hello”, self.hello, methods=[“GET”]) def hello(self): return {“Hello”: self.name} app = FastAPI() hello = Hello(“World”) app.include_router(hello.router) Example: $ curl 127.0.0.1:5000/hello {“Hello”:”World”} add_api_route‘s second argument (endpoint) has type Callable[…, Any], … Read more

FastAPI – GET request results in typeerror (value is not a valid dict)

Pydantic 2 changed how models gets configured, so if you’re using the most recent version of Pydantic, see the section named Pydantic 2 below. SQLAlchemy does not return a dictionary, which is what pydantic expects by default. You can configure your model to also support loading from standard orm parameters (i.e. attributes on the object … Read more

Is there a way to kill uvicorn cleanly?

That’s because you’re running uvicorn as your only server. uvicorn is not a process manager and, as so, it does not manage its workers life cycle. That’s why they recommend running uvicorn using gunicorn+UvicornWorker for production. That said, you can kill the spawned workers and trigger it’s shutdown using the script below: $ kill $(pgrep … Read more

is there a difference between running fastapi from uvicorn command in dockerfile and from pythonfile?

Update (on 2022-12-31) As an update from @Marcelo Trylesinski, from uvicorn v 0.19.0, the –debug flag was removed (Ref #1640). No, there is no difference. The commadline run method (uvicorn app.main:app) and executing the app.py using python command (python app.py) are the same. Both methods are calling the uvicorn.main.run(…) function under the hood. In other … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)