OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

FastAPI: How How to get request(Request object) in route decorator

  • Thread starter Thread starter Steph
  • Start date Start date
S

Steph

Guest
FastAPI: How How to get request(Request object) in route decorator?

what i want?

  1. how to get a request object in the decorator without specifying it explicitly in route

how works: example:

Code:
def decorator_func(...):
    def decorator(func):
        @wraps(func)
        async def wrapper(request, *args, **kwargs):
            return await func(args, kwargs)

        return wrapper

    return decorator


@router.get(path='/something')
@decorator_func(...)
def get_something(request: Request):
    ...

how i want: example

Code:
def decorator_func(...):
    def decorator(func):
        @wraps(func)
        async def wrapper(*args, **kwargs):
            request = ... # GET request object
            return await func(args, kwargs)

        return wrapper

    return decorator


@router.get(path='/something')
@decorator_func(...)
def get_something(...):
    ...
<p>FastAPI: How How to get request(Request object) in route decorator?</p>
<p>what i want?</p>
<ol>
<li>how to get a request object in the decorator without specifying it explicitly in route</li>
</ol>
<p>how works: example:</p>
<pre><code>def decorator_func(...):
def decorator(func):
@wraps(func)
async def wrapper(request, *args, **kwargs):
return await func(args, kwargs)

return wrapper

return decorator


@router.get(path='/something')
@decorator_func(...)
def get_something(request: Request):
...
</code></pre>
<p>how i want: example</p>
<pre><code>def decorator_func(...):
def decorator(func):
@wraps(func)
async def wrapper(*args, **kwargs):
request = ... # GET request object
return await func(args, kwargs)

return wrapper

return decorator


@router.get(path='/something')
@decorator_func(...)
def get_something(...):
...
</code></pre>
 

Latest posts

I
Replies
0
Views
1
impact christian
I
Top