[BUG] URL slash encoded
See original GitHub issueDescribe the bug
FastAPI is not routing URL containing slash encoded.
To Reproduce
- Create a file with:
from fastapi import FastAPI
app = FastAPI()
@app.get("/units/{unit}")
def read_unit(unit: str):
return {"unit": unit}
- Open the browser and call the endpoint
/units/kg%2Fs
. - It returns Status 404 with JSON with
{"detail":"Not Found"}
. - But I expected it to return
{"unit": "kg/s"}
. - Working with
/units/kg
, returning{"unit": "kg"}
.
Expected behavior
FastAPI working with URL encoded slash
Environment
- OS: Windows
- FastAPI Version : 0.45.0
- Python 3.7.3
Additional context
Starlette regexp seems fine though when testing it returns this route regexp:
'^/units/(?P<unit>[^/]+)$'
>>> import re
>>> pattern=re.compile('^/units/(?P<unit>[^/]+)$')
>>> pattern.match('/units/kg%2Fs')
<re.Match object; span=(0, 13), match='/units/kg%2Fs'>
>>> pattern.match('/units/kg%2Fs').groupdict()["unit"]
'kg%2Fs'
Issue Analytics
- State:
- Created 4 years ago
- Comments:16 (4 by maintainers)
Top Results From Across the Web
urlencoded Forward slash is breaking URL - Stack Overflow
I am using urlencode() while preparing the search URL ... I first replace the slash by html entity and then I do the...
Read more >URL-Encoding of "slashes" - Online
Encode "slashes" to URL-encoded format. Simply enter your data then push the encode button. To encode binaries (like images, documents, etc.)
Read more >5 Solutions to Url Encoded Slashes (%2F) Problem in Apache
The safest and most practical way of handling slashes, is to replace them with underscores before url encoding. Using the variables in #2,...
Read more >(backslash) should be encoded as %5C in the URL query
The url-bar still decodes characters and turns valid characters into invalid ones (and sometimes it turns valid URLs into different valid URLs by...
Read more >[SOLVED] URL-encoded slash ("/") character does not work
404 Not Found The application could not run because of the following error: Details Type: Slim\Exception\HttpNotFoundException Code: 404 ...
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
@Gatsby-Lee
If you want to keep this kind of behavior with slash in your routing path. You can use this workaround with a specific character to split your url, like
-
or#
. We can do that with starlette with the routing convertorspath
(https://www.starlette.io/routing/).Keeping my example, you can try the following:
You can now visit the URL
http://127.0.0.1:8000/units/-/kg/s
which will outputs :@jules-ch I came across this issue and tried your suggestion for something similar and it worked. My use case was returning the name of a specific interface in a network router. Network routers have names with slashes, like et-1/0/0 (Ethernet HundredGig in this case). This means the URL needs to look like et-1%2f0%2f0, as %2f is for a / character.
With the route
the URL sent to FastAPI would be http://1.2.3.4:8000/cfg/interfaces/interface/et-1/0/0 which is an incorrect route resulting in a 404 Not Found.
I followed your suggestion with adding
:path
and the route worked.The function was able to parse XML using xpath, using that variable for the interface name.
Just confirming and providing an additional example of anyone that sees this.