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.

[BUG] URL slash encoded

See original GitHub issue

Describe the bug

FastAPI is not routing URL containing slash encoded.

To Reproduce

  1. Create a file with:
from fastapi import FastAPI
app = FastAPI()


@app.get("/units/{unit}")
def read_unit(unit: str):
    return {"unit": unit}

  1. Open the browser and call the endpoint /units/kg%2Fs.
  2. It returns Status 404 with JSON with {"detail":"Not Found"}.
  3. But I expected it to return {"unit": "kg/s"}.
  4. 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:closed
  • Created 4 years ago
  • Comments:16 (4 by maintainers)

github_iconTop GitHub Comments

8reactions
jules-chcommented, Dec 10, 2020

@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 convertors path (https://www.starlette.io/routing/).

Keeping my example, you can try the following:


from fastapi import FastAPI

app = FastAPI()


@app.get("/units/-/{unit:path}")
def read_unit(unit: str):
    return {"unit": unit}

You can now visit the URL http://127.0.0.1:8000/units/-/kg/s which will outputs :

{"unit":"kg/s"}
5reactions
Menotomycommented, Mar 10, 2021

@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

@app.get('/cfg/interfaces/interface/{int_name}')
def get_int(int_name: str):

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.

@app.get('/cfg/interfaces/interface/{int_name:path}')
def get_int(int_name: str):

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.

Read more comments on GitHub >

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

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