简体   繁体   中英

Connexion/Flask application: get base_path from request

I have a connexion/flask/werkzeug application and I need to be able to obtain the "base_path" during requests. For example: my application is available at: http://0.0.0.0:8080/v1.0/ui/#/Pet , with the base_path being: "http://0.0.0.0:8080/v1.0".

I want to be able to get the base_path when the requestor performs any defined operation (GET, POST, PUT, etc). I have not been able to find an easy way to obtain the base path. Through a python debugger, I can see the base_path is available higher up in the stack but doesn't appear to be available to the application entrypoint.



#!/usr/bin/env python3
import connexion
import datetime
import logging

from connexion import NoContent


PETS = {}


def get_pet(pet_id):
    pet = PETS.get(pet_id)

    # >>>--> I WANT TO GET THE BASE_PATH OF THE REQUEST HERE <--<<<

    return pet or ('Not found', 404)


For reasons I can not detail due to nda, I have multiple openapi specs for this application and it's important for me to know which base_path is being requested (as they are different). If somebody could help me figure out a way to obtain the base_path per request I would be greatly appreciative:)

Thank you!

Use connexion.request.base_url . https://connexion.readthedocs.io/en/latest/request.html#header-parameters you can access the connexion.request inside your handler

refer to this topic ( Incoming Request Data ) from Flask documentation

and dump the incoming request with the before_request hook and extract the right one eg request.base_url for your case:

from flask import .., request

@bp.before_request
def dump_incoming_request():

    from pprint import pprint
    pprint(request.__dict__.items())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM