Routes with trailing slash do not work under default API Options
See original GitHub issueHello,
It seems when using the current default for api.req_options.strip_url_path_trailing_slash which is False, adding a slash at the end of a route doesn’t work.
I think I found out what the problem is.
Here is the code I tried:
import falcon
class StaticResource(object):
def on_get(self, _: falcon.Request, resp: falcon.Response) -> None:
resp.status = falcon.HTTP_200
resp.content_type = 'text/plain'
resp.body = 'static'
def create_app() -> falcon.API:
api = falcon.API()
api.req_options.strip_url_path_trailing_slash = False
api.add_route('/static_route/', StaticResource())
api.add_route('/static_route2', StaticResource())
print(api._router._finder_src)
return api
The finder_src compiled function is :
def find(path, return_values, patterns, converters, params):
path_len = len(path)
if path_len > 0:
if path[0] == 'static_route':
if path_len == 1:
return return_values[0]
return None
if path[0] == 'static_route2':
if path_len == 1:
return return_values[1]
return None
return None
return None
We see that the code generated for both routes is the same. The path passed to this function when calling uri: “http://127.0.0.1:8000/static_route/” is : [‘static_route’, ‘’] which has a length of 2, leading the function to return None and therefore an HTTPError 404. When calling uri: “http://127.0.0.1:8000/static_route”, the function returns the correct route and leads to HTTP 200.
Therefore using the default option leads to an impossibility to have routes ending with a trailing slash and even worse, when using routes ending with a trailing slash, the route without it works…
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:8 (6 by maintainers)
Hello, we experienced this issue as well. We were on version 1.4.1 and one of the routes was defined with a / in the end. It used to work with and without the slash when queried. However, after upgrading, querying with a / returns a 404.
Hi @laurent-chriqui, thanks for submitting a bug report, we really appreciate when it includes code!
I am not sure I understand what you expect. Note that,
req_options.strip_url_path_trailing_slash
controls how theRequest
segments are determined, not how routing works, they are independent ideas.Consider a route
add_route('/home/', HomeResc())
andadd_route('/home/{param}, Dashboard())
.How would you route a request to
/home/data
vs/home/
withstrip_url_path_trailing_slash=False
? Should/home/
go toHomeResc
or should it goDashboard
? I think there is an argument it should go toDashboard
withparam=""
and that is what it does. Here is a quick example:This changes slightly with
strip_url_path_trailing_slash=True
:This might point to us needing to
raise
when there is a trailing slash passed toadd_route
since it is ultimately meaningless.Morale of the story
strip_url_path_trailing_slash
is about theRequest
handling not about the routers behavior.I am going to mark this as needing documentation here. If the other maintainers think there are code level changes for this… or if I am patently wrong (that happens far more often than I like!) please let me know!
Bonus… if you would like it to be different you can change it! 🎉
Bonus 2… If you are actually trying to serve statics, you might want to look at
add_static_route
. If it doesn’t meet your needs, let us know, we might be able to add features!Bonus 3… Thanks again for submitting this!