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.

Use "308 Permanent Redirect" for redirect slashes behavior.

See original GitHub issue

Hi, 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:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
hjkellycommented, May 5, 2019

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, but POST loses its data and is changed to a GET.

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.

0reactions
tomchristiecommented, May 23, 2019

@tibudiyanto Absolutely agreed. No particular need for us to allow it to be configurable either - it’s clearly the wrong code as it stands.

Read more comments on GitHub >

github_iconTop 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 >

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