Ravi Bhushan Ravi Bhushan - 1 year ago 109
Python Question

How can i get current base URI in flask?

In below code, i want to store URL in a variable to check error on which URL error occured.

@app.route('/flights', methods=['GET'])
def get_flight():
flight_data= mongo.db.flight_details
info = []
for index in flight_data.find():
info.append({'flight_name': index['flight_name'], 'flight_no': index['flight_no'], 'total_seat': index['total_seat'] })
if request.headers['Accept'] == 'application/xml':
template = render_template('data.xml', info=info)
xml_response = make_response(template)
xml_response.headers['Accept'] = 'application/xml'
logger.info('sucessful got data')
return xml_response
elif request.headers['Accept'] == 'application/json':
logger.info('sucessful got data')
return jsonify(info)


* Restarting with stat
* Debugger is active!
* Debugger PIN: 165-678-508
* Running on (Press CTRL+C to quit) - - [28/Mar/2017 10:44:53] "GET /flights HTTP/1.1" 200 -


i want this message
" - - [28/Mar/2017 10:44:53] "GET /flights HTTP/1.1" 200 -"
should be stored in a variable or how can i get current URL that is executing.
Thank You

Answer Source

You can use the base_url method on flask's request function.

from flask import Flask, request

app = Flask(__name__)

def index():
    return request.base_url

if __name__ == '__main__':

This returns the following if the app route is /foo:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download