Python/FastAPI: how can I get headers or a specific header from my backend API?

We Are Going To Discuss About Python/FastAPI: how can I get headers or a specific header from my backend API?. So lets Start this Python Article.

Python/FastAPI: how can I get headers or a specific header from my backend API?

  1. How to solve Python/FastAPI: 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, you can try out sending a curl
    curl -H "My-Header: test" -X GET http://localhost:8000
    This will result in
    {"message":"test"}
    UPD:
    if you need to access it in decorator you can use following
    def token_required(func): @wraps(func) async def wrapper(*args, request: Request, **kwargs): my_header = request.headers.get('my-header') # my_header will be now available in decorator return await func(*args, request, **kwargs) return wrapper

  2. Python/FastAPI: 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, you can try out sending a curl
    curl -H "My-Header: test" -X GET http://localhost:8000
    This will result in
    {"message":"test"}
    UPD:
    if you need to access it in decorator you can use following
    def token_required(func): @wraps(func) async def wrapper(*args, request: Request, **kwargs): my_header = request.headers.get('my-header') # my_header will be now available in decorator return await func(*args, request, **kwargs) return wrapper

Solution 1

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, you can try out sending a curl

curl -H "My-Header: test" -X GET http://localhost:8000

This will result in

{"message":"test"}

UPD:

if you need to access it in decorator you can use following


def token_required(func):
    @wraps(func)
    async def wrapper(*args, request: Request, **kwargs):
        my_header = request.headers.get('my-header')
        # my_header will be now available in decorator
        return await func(*args, request, **kwargs)
    return wrapper


Original Author ihoryam Of This Content

Solution 2

Or, as described in the fastapi documentation (https://fastapi.tiangolo.com/tutorial/header-params/):

from typing import Optional

from fastapi import FastAPI, Header

app = FastAPI()


@app.get("/items/")
async def read_items(user_agent: Optional[str] = Header(None)):
    return {"User-Agent": user_agent}

this will fetch the user_agent header parameter.

Original Author Valentin Dumitru Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

ittutorial team

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment