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.

307 on a delete for a post request.

See original GitHub issue

Describe the bug

When a delete is made on a post route with a trailing slash the server returns a 307 temporary redirect. I would either expect a 404 not found or a 405 method not allowed.

To Reproduce

Just a unittest to show the case:

from fastapi import FastAPI, Request, Response
from fastapi.testclient import TestClient

app = FastAPI()


@app.post("/bla")
async def bla():
    return {}


client = TestClient(app)


def test_read_main():
    response = client.delete("/bla/")
    print(response.headers)
    assert response.status_code == 405

Environment

  • OS: all
  • FastAPI Version: 0.53.2
  • Python Version: 3.8.2

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

8reactions
vjprcommented, Apr 8, 2020

Just had a look into this. The main FastAPI instance has an APIRouter.

When you create separate fastapi.routing.APIRouter instances and then use app.include_router(...), behind the scenes it is actually appending the routes from this router to FastAPI#router.

This should work:

app = FastAPI()
app.router.redirect_slashes = False

Seems that redirect_slashes means “redirect paths without trailing slash to slash”, NOT “redirect trailing slash to no slash”.

starlette/routing.py:601

        if scope["type"] == "http" and self.redirect_slashes:
            if not scope["path"].endswith("/"):
                redirect_scope = dict(scope)
                redirect_scope["path"] += "/"

Also, if you are using a FastAPI or Flask backend via a proxy (like in CRA setupProxy.js with http-proxy-middleware), then you have some additional work to do as described here: https://github.com/chimurai/http-proxy-middleware/issues/140#issuecomment-611237707

4reactions
Dustyposacommented, Apr 3, 2020

Your path has some error. you should use :response = client.delete("/bla")

Read more comments on GitHub >

github_iconTop Results From Across the Web

307 Temporary Redirect - HTTP - MDN Web Docs
HTTP 307 Temporary Redirect redirect status response code indicates that the resource requested has been temporarily moved to the URL given ...
Read more >
What is the HTTP 307 Temporary Redirect Status Code - Kinsta
The 307 status code indicates that the target resource resides temporarily under a different URI. Find out more with this in-depth guide.
Read more >
307 Temporary Redirect: What It Is and How to Fix It
A 307 Temporary Redirect message is an HTTP response status code indicating that the requested resource has been temporarily moved to ...
Read more >
How to remove post parameters on temporary redirect (HTTP ...
I have a problem with redirecting requests. I have an incoming GET or POST for http://foo.com/bar?A=b . I redirect it with HTTP 307...
Read more >
How to redirect a HTTP put, post or delete requests and keep ...
302 and 307 are temporary redirects, this means that the client should not cache the redirect and should request the original resource next...
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