Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

accessing the request object #19

Closed
euri10 opened this issue Feb 1, 2019 · 4 comments
Closed

accessing the request object #19

euri10 opened this issue Feb 1, 2019 · 4 comments

Comments

@euri10
Copy link
Contributor

euri10 commented Feb 1, 2019

In starlette you can access request object in function decorated with the route decorator.

it seems very handy to be able to access middlewares etc,
is there a way in fastapi to do that using the provided get/post/options.... decorators?
same question for the ApiRouter.

@app.route("/notes", methods=["GET"])
async def list_notes(request):
    query = notes.select()
    results = await request.database.fetchall(query)
@tiangolo
Copy link
Owner

tiangolo commented Feb 1, 2019

Yes! It's not properly documented yet.

It's pretty much what (I guess) you would expect. Declare it with a Starlette Request type and FastAPI will provide it:

from fastapi import FastAPI
from starlette.requests import Request

app = FastAPI()


@app.get("/")
def read_root(request: Request):
    query = notes.select()  # Not sure where `notes` comes from
    results = await request.database.fetchall(query)
    return {"Hello": results}

@euri10
Copy link
Contributor Author

euri10 commented Feb 1, 2019 via email

@tiangolo
Copy link
Owner

tiangolo commented Feb 1, 2019

🎉 🌮

@github-actions
Copy link
Contributor

Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs.

@tiangolo tiangolo reopened this Feb 28, 2023
Repository owner locked and limited conversation to collaborators Feb 28, 2023
@tiangolo tiangolo converted this issue into discussion #8330 Feb 28, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests

2 participants