question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUESTION] Accepting both GET and POST for the same route

See original GitHub issue

Hi there,

I’ve recently moved to FastAPI from Flask, and love the smoothness and speed of FastAPI. However, one thing I’m still trying to work out is how to accept GET and POST requests for the same routing, and to direct them both to the same function. In Flash I used to just use app.route and check the response.method.

For example, if I have some longFunction(request: Request), I’d like to route both GET and POST requests to it, but the decorator pattern seems to only allow one of these.

I’ve also tried the following, also to no avail (throws TypeError(“‘coroutine’ object is not iterable”)):

@app.get("/"):`
    async def dummyFunction(request: Request):
        return longFunction(request)
@app.post("/"):`
    async def longFunction(request):
        ...

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6

github_iconTop GitHub Comments

6reactions
TheLiamGuycommented, Oct 17, 2020

Looks like double decorators do work after all. My apologies

0reactions
Krzyzaku21commented, Jun 15, 2022

I do something that to only request get method. But I’m use FASTAPI second day, and I’m not sure this one

def default_get(path: str, url: str, name: str):
    @base_router.get(path=path, response_class=HTMLResponse)
    def function(request: Request):
        function.__name__ = name
        return settings.templates.TemplateResponse(
            url,
            context={
                "request": request,
            },
        )


default_get("/", "index.html", "homepage")

default_get("/register", "users/register.html", "register_get")
Read more comments on GitHub >

github_iconTop Results From Across the Web

Handle both GET and POST request with same route in .net ...
Have you tried to create two separate functions. One for GET and the other for POST? You can still set the Route attribute...
Read more >
Handle GET and POST Request in Express - CodeForGeek
GET and POST is two common HTTP Requests used for building REST APIs. Both of these calls ... We will use Express Router...
Read more >
Difference between GET and POST Request in HTTP ... - Java67
GET, and POST is two of the most common HTTP methods you would hear or work on the web. Though both can be...
Read more >
HTTP Request Methods – Get vs Put vs Post Explained with ...
In this article, we'll be discussing the get, put, and post HTTP methods. You'll learn what each HTTP method is used for as...
Read more >
GET vs POST - Difference and Comparison | Diffen
HTTP POST requests supply additional data from the client (browser) to the ... The submission process for both methods begins in the same...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found