-1

I have a question about Flask.

I want to use one endpoint to handle requests.

To do so, I need to take url in router like:

@app.route("/<url>", methods=['GET','POST'])
def home(url):
    base_url = "https://www.virustotal.com/"
    my_url = base_url + url

For example, I will sent request to my Flask app as " localhost:5000/api/v3/files/samehashvalue " and it will combine it with virustotal url.

my_url will something like = virustotal.com/api/v3/files/samehashvalue

How can I pass /api/v3... to my router? Without using parameters like ?url=...

Goxo
  • 39
  • 4

2 Answers2

0

I'd suggest reading redirects from the Flask docs as well as url building.

With your specific example you can obtain the url from the route and pass it into your Flask function. It's then just a case of using redirect and an f string to redirect to that new url.

# http://localhost:5000/test redirects to 
# https://www.virustotal.com/api/v3/files/test


from flask import Flask, redirect

app = Flask(__name__)

@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def url_redirector(path):
    return redirect(f'https://www.virustotal.com/api/v3/files/{path}')

if __name__ == '__main__':
    app.run(debug=True)
Johnny John Boy
  • 3,009
  • 5
  • 26
  • 50
  • sorry but /api/v3/files/ is not fixed url it can be like /api/v3/urls etc. so it varies. how can i handle it like dynamic route? – Goxo Jul 28 '22 at 11:50
  • Try now.. remember though that path:path matches everything so if you redirect to localhost:5000 it will catch that and end up being a recursive and break :-) – Johnny John Boy Jul 28 '22 at 12:08
-1

I am not sure if this is correct, but I assume that you can specify the path in @app.route if it is a fixed path. For example:

@app.route("/api/v3/files", methods=['GET','POST'])
def home(url):
    base_url = "https://www.virustotal.com/api/v3/files/"

Then the hash value only can be passed as a parameter.

Please let me know if I misunderstood your question and I will edit this answer.

  • sorry but path is not fixed and it varies. sometimes it will be /api/v3/files, or /api/v3/urls etc. also /api/v3 can be changed to v4 in future. so i want use one router and take url (/api/v3...) and combine it to virustotal.com/ – Goxo Jul 28 '22 at 11:42