Skip to content

Instantly share code, notes, and snippets.

@moreati
Last active June 22, 2021 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moreati/048358ecf9946dce7e882873bd697541 to your computer and use it in GitHub Desktop.
Save moreati/048358ecf9946dce7e882873bd697541 to your computer and use it in GitHub Desktop.
How do Python webframeworks handle URL-encoded / in the path?
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def root():
return {"greeting": "Hello"}
@app.get("/{foo}/{bar}")
async def foobar(foo: str, bar: str):
return {"foo": foo, "bar": bar}
# $ python3 -m virtualenv v-fastapi
# $ v-fastapi/bin/pip install fastapi uvicorn[standard]
# $ v-fastapi/bin/uvicorn fastapi_app:app --reload
# ...
# INFO: 127.0.0.1:49990 - "GET /a/c/b HTTP/1.1" 404 Not Found
# $ curl "http://127.0.0.1:8000/a%2fc/b"
# {"detail":"Not Found"}
from flask import Flask
app = Flask(__name__)
@app.get("/")
def root():
return "Hello\n"
@app.get("/<foo>/<bar>/")
def foobar(foo, bar):
return f"{foo=} {bar=}\n"
# $ python3 -m virtualenv v-flask
# $ v-flask/bin/pip install flask
# $ FLASK_APP=flask_app v-flask/bin/flask run --reload
# ...
# 127.0.0.1 - - [22/Jun/2021 18:41:41] "GET /a%2fc/b HTTP/1.1" 404 -
# $ curl "http://127.0.0.1:5000/a%2fc/b"
# <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
# <title>404 Not Found</title>
# <h1>Not Found</h1>
# <p>The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.</p>
from quart import Quart
app = Quart(__name__)
@app.route("/")
async def root():
return "Hello\n"
@app.route("/<foo>/<bar>")
async def foobar(foo, bar):
return f"{foo=} {bar=}\n"
# $ python3 -mvirtualenv v-quart
# $ v-quart/bin/pip install quart
# $ QUART_APP=quart_app:app v-quart/bin/quart run
# ...
# [2021-06-22 19:25:57,594] 127.0.0.1:42388 GET /a/c/b 1.1 404 232 1250
# $ curl "http://127.0.0.1:5000/a%2fc/b"
# <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
# <title>404 Not Found</title>
# <h1>Not Found</h1>
# <p>The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.</p>
from sanic import Sanic, response
app = Sanic(__name__)
@app.get("/")
async def root(request):
return response.text("Hello\n")
@app.get("/<foo>/<bar>")
async def foobar(request, foo, bar):
return response.text(f"{foo=} {bar=}\n")
# $ python3 -m virtualenv v-sanic
# $ v-sanic/bin/pip install sani
# $ v-sanic/bin/sanic --debug sanic_app.app
# ...
# [2021-06-22 19:06:29 +0100] - (sanic.access)[INFO][127.0.0.1:50462]: GET http://127.0.0.1:8000/a%2fc/b 200 20
# $ curl "http://127.0.0.1:8000/a%2fb/c/"
# foo='a%2fb' bar='c'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment