Use "308 Permanent Redirect" for redirect slashes behavior.
See original GitHub issueHi,
I stumbled upon a quirk in starlette that is not properly documented. It seems like all of my HTTP request to a route without trailing slash are being redirected to route with trailing slashes. Say I am hitting http://hostname/mountpoint/api
it will be redirected (302) to http://hostname/mountpoint/api/
. This messed up your api calls; if you call POST to http://hostname/mountpoint/api
it will be redirected to GET http://hostname/mountpoint/api/
.
I dig on the source and see this redirect_slashes flag and setting it to False fix this. I feel this behavior (the auto redirection) should be documented.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:6 (4 by maintainers)
Top Results From Across the Web
308 Permanent Redirect: What It Is and How to Fix It
A 308 Permanent Redirect response code indicates that the requested resource has been permanently moved to the new URI specified in the Location...
Read more >Pytest Flask, error 308 Permanent Redirect when login
Calls without the slash get matched to a 308 redirect that is automatically created when you create a route with a trailing slash....
Read more >308 Permanent Redirect - HTTP - MDN Web Docs
The HyperText Transfer Protocol (HTTP) 308 Permanent Redirect redirect status response code indicates that the resource requested has been ...
Read more >HTTP redirect codes for SEO explained - ContentKing
What HTTP redirect code to use in what situation? In this guide, we'll explain all the redirect types with their pros and cons!...
Read more >Redirects - Screaming Frog
“If you need to change the URL of a page as it is shown in search engine results, we recommend that you use...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m new to fastapi, and I think I’m running into this issue as well. If I forget a necessary slash, I’m redirected, which has no side effects for
GET
, butPOST
loses its data and is changed to aGET
.I’m new to this tool, but I’ve used Django for a decade, as well as django-rest-framework. Django’s
APPEND_SLASH=True
breaks non-GET
requests. django-rest-framework provides routers for their viewsets, but you have to override the__init__
method to make trailing slashes optional. Even then,@action
subresources must have trailing slashes. In my opinion, that functionality has always seemed broken.Instead of modifying the docs to clarify this, what if the routes were forgiving of slash confusion? Instead of 404ing or even redirecting,
compile_path
could just ensure there’s a/?
instead of/
or no slash at all. I suggest this because I’ve made incorrectly-slashed requests hundreds of times over the years, and doing this with POST requests can result in confusion or even lost data. In contrast, it seems unlikely that someone would miss the ability to specify routes only with trailing slashes, or _only when there is no trailing slash.Let me know what you think or if I can help.
@tibudiyanto Absolutely agreed. No particular need for us to allow it to be configurable either - it’s clearly the wrong code as it stands.