2

I'm trying to build a cors proxy through flask from scratch. Here's my code

@app.route('/api/v1/cors/url=<name>&method=<method>', methods=['GET'])
def api_cors(name, method):
    if method == 'http' or method == 'https':
        r = request.urlopen(method+"://"+name)
        return r.read()
    else:
        return "method not set!"

It is working good so far but I have one problem, when I pass "url=google.com&method=https" it is working fine but when I pass something like "url=google.com/images/image.jpg&method=https" the "/" will considered as a new directory

Is there anyway to evade this in flask?

Maximilian Peters
  • 30,348
  • 12
  • 86
  • 99
Val G.
  • 131
  • 1
  • 13

2 Answers2

4

Don't try and pass the value as part of the route itself. Pass it as a query parameter.

@app.route('/api/v1/cors/')
def api_cors():
    url = request.args.get('url')

and call it as "/api/v1/cors/?url=https://google.com/images/image.jpg".

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
2

If you want use the same URL scheme that you're using now, change your routing decorator to this and it will work.

@app.route('/api/v1/cors/url=<path:name>&method=<method>', methods=['GET'])
Jake Conway
  • 901
  • 16
  • 25