-1

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.

10Nates
  • 102
  • 10
  • Does this answer your question? [Proxying to another web service with Flask](https://stackoverflow.com/questions/6656363/proxying-to-another-web-service-with-flask) – Ken Kinder Jul 14 '20 at 22:14
  • No, from what I can tell. The link you send does essentially the same thing I've already found. I want something that will reroute within Flask instead of making a GET request, waiting for it to finish, and then sending the data. – 10Nates Jul 15 '20 at 00:12
  • flask wasn't created for this - but for generating pages. And normally you should run it with nginx/apache which should redirect it (and even server static files) because it makes it faster. But even nginx/apache has to read it and send it like in your code. BTW if this is url in the same application then don't use `requests` but run directly function which generate this page. – furas Jul 15 '20 at 00:50
  • BTW: I don't know if this is solution for you but you can send HTML with ` – furas Jul 15 '20 at 00:54
  • furas, do you have an example of how do call the function from within another (& include arguments if possible)? Because that sounds exactly like what I want. – 10Nates Jul 15 '20 at 02:01
  • I'm not sure I follow. To to clear, there is no such thing as "redirecting without changing the URL." By definition, a redirect is to a new URL. So I'm imagining that perhaps you mean you want Flask to proxy a request to another server, and then serve that content. – Ken Kinder Jul 15 '20 at 14:36

1 Answers1

1

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

10Nates
  • 102
  • 10