Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

265
Visualizações
Flask: remove end of line characters from url

i need to remove the end of line characters from incoming requests in flask. If the route is https://localhost/test\r\n I need to remove \r\n. This is due to an issue from a oher program, that is alwas sending these. So is there a way to make this neater?

@app.route("/test")
@app.route("/test\r\n")
def test():
    return "TEST"

Thanks in advance

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You could implement a simple WSGI middleware that fixes the URL before they are processed by Flask. That might look something like:

from flask import Flask


class FixURLMiddleware:
    def __init__(self, app):
        self.app = app

    def __call__(self, environ, start_response):
        environ["PATH_INFO"] = environ["PATH_INFO"].strip()
        return self.app(environ, start_response)


app = Flask(__name__)
app.wsgi_app = FixURLMiddleware(app.wsgi_app)


@app.route("/hello")
def hello():
    return "Hello, world"


if __name__ == "__main__":
    app.run()

For any incoming request, this will call the strip() method (which removes whitespace, including newlines and carriage returns, from the start and end of strings) on the path before the request is handled by Flask.

over 4 years ago · Santiago Trujillo Relatório

0

You can use middleware to solve the issue, as you can wrap your application and modify input and output before the application.

Here is a working example of such:

from flask import Flask

app = Flask(__name__)


class PathNormaliser:
    """
    Simple WSGI middleware
    """
    def __init__(self, app):
        self._app = app

    def __call__(self, environ, start_response):
        environ['PATH_INFO'] = environ['PATH_INFO'].replace("\\r", "").replace("\\n", "")
        environ['REQUEST_URI'] = environ['REQUEST_URI'].replace("\\r", "").replace("\\n", "")
        environ['RAW_URI'] = environ['RAW_URI'].replace("\\r", "").replace("\\n", "")
        return self._app(environ, start_response)


app.wsgi_app = PathNormaliser(app.wsgi_app)


@app.route("/")
def hello_world():
    return "<p>Hello, World!</p>"


app.run()

You can read more about it in a thread similiar to your case here: modify flask url before routing

Or alternatively, you can read about WSGI middleware here: https://flask.palletsprojects.com/en/2.0.x/api/?highlight=middleware#flask.Flask.wsgi_app

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda