Reputation: 505
I'm trying to create a URL prefix for my web app's api. I want the api to return when I enter api.website.com/parameter.I am using Flask and Blueprint
api_bp = Blueprint('api', __name__,
template_folder='templates',
url_prefix='/api')
@api_bp.route("/")
def get_monkey_api():
address = request.args.get('address',
None)
if address and is_a_banano_address(address):
return monkey_api(banano_address)
else:
return render_template("NotABananoAddress.html"), 400
@api_bp.route("/<address>")
def monkey_api(address):
monKey = monkey_data_generator.generate_monKey(address)
if monKey.status is not 'Citizen':
return "API data on vanity monKeys does not exist"
return jsonify(monKey.serialize())
app = Flask(__name__)
app.register_blueprint(api_bp, url_prefix='/api')
Most of the code in unrelated. The fact is when I I am entering
api.website.com?address=xxx
or when I am entering
api.website.com/xxx
I should get my API as JSON back but I'm not. On localhost it doesn't return anything and doesn't show the prints that I even insert into the code and of course on Heroku it does not recognize the project when I using the prefix.
Upvotes: 1
Views: 970
Reputation: 1122152
You gave your blueprint a URL prefix:
api_bp = Blueprint('api', __name__,
template_folder='templates',
url_prefix='/api')
and again with
app.register_blueprint(api_bp, url_prefix='/api')
That means you need to use hostname/api/
to get to get_monkey_api()
view function, or hostname/api/xxxx
to get to the monkey_api()
view function.
Remove the URL prefixes if you want the routes to be found at the site root. If you want the blueprint to work for a separate subdomain, then use the subdomain='api'
option, not a URL prefix.
Note that for subdomains to work, you need to configure the SERVER_NAME
config option so that subdomains can be detected. If you want to test this locally, edit your /etc/hosts
file to add some development aliases that point to your server, then set SERVER_NAME
to match.
Upvotes: 1