Search code examples
pythonpython-3.xflaskget

How do I redirect within Flask without changing the URL


How do I redirect within Flask without changing the URL? I've looked for while now and haven't found a good solution. There are other threads for .htaccess and nginx but they aren't applicable to this situation

The only solution I have is:

# url_root is currently http://127.0.0.1:5000/
# args = t/Some%20text?p=preset

res = requests.get(request.url_root + args)
return res.text, res.status_code, res.headers.items()

It's essentially just visiting the url, then copying the data and sending it. While this does work, it is very slow, around 300-400ms wait time on a local server.

I feel like there's got to be some way to reroute the request, but I can't find it.

Edit: I am not looking for a basic Flask proxy, as that does the same thing that I am already doing. It contains the same bottleneck that I want to avoid. I want something that will call a different page from within Flask.


Solution

  • I ended up doing the following:

    @app.route('/t/<urltext>', defaults={'args': None})
    def index(urltext, args):
        # Stuff and things
    
    # later...
    @app.route('/l/<urlcode>')
    def little(urlcode):
        # Processing urlcode
        return index(urltext=text, args=args)
    

    It's basically just avoiding the arguments problem but it works