-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Allow disabling appending slash? #1246
Comments
|
That actually makes the route match when the slash is missing. I want it to 404. |
The current behavior is:
I want a 404 when the rule has a trailing slash but the path doesn't. Right now I'm doing this via: class StrictRule(Rule):
def match(self, path, method=None):
try:
result = super(StrictRule, self).match(path, method)
except RequestSlash:
return None
return result But I'd rather not reach into internals. |
Using the custom rule would be the correct way to do this in Flask. We have decided internally not to try and create everything for everyone in rules. If you want a rule beyond what is offered as standard you would need to specify it. For Flask, you would set the |
Per #1246 (comment), the issue here is with the custom rule implementation. werkzeug/src/werkzeug/routing.py Lines 259 to 260 in a220671
And while I certainly agree that base rules shouldn't support every possible use case, not matching on an absent trailing slash is a very common use case. For prior art, this pattern is one of the few that are explicitly exposed in Django config via |
"Internal" in this case means "internally handled," not "for "internal use only." The code you've shown is fine to use. |
@davidism Thank you for the clarification. I'll go ahead and go with this, then. |
We're using Werkzeug and Flask to build a REST API. In this context, we don't want the automatic behavior of appending trailing slashes, as this leads to asymmetries between GETs and other requests, and it's acceptable to be strict here.
Currently we need to use a custom rule class that hooks into internals (
Rule.match
andRequestSlash
) to do this.Would it make sense to add this as a first-order config option? Something like
append_slash
(to parallel the equivalent Django option) on URL maps?The text was updated successfully, but these errors were encountered: