flask-restx
flask-restx copied to clipboard
Indicate Base URL in Swagger UI
***** SIMILAR ISSUE IN flask-restplus *****
Code
from flask import Flask
from flask_restx import Api
app = Flask(__name__)
api = Api(app,
title='Data Quality Framework API',
version='v0.1',
doc='/documentation',
base_url='/test')
@api.route('/', methods=["GET", "POST"])
def home():
response = make_response(jsonify('Success'), 200)
response.headers["Content-type"] = "application/json"
return response
Expected Behavior
When visiting the '/' we expect to get "Success"
Actual Behavior
Error 404. Page not found
Error Messages/Stack Trace
"GET / HTTP/1.1" 404 -
Correct me if I am wrong, but the behavior that you are seeing is expected. As from my understanding your line
api = Api(app,
title='Data Quality Framework API',
version='v0.1',
doc='/documentation',
base_url='/test')
is dictating that all paths to your endpoint should be prefixed with /test. So if you just go to / you are bound to get a 404 as that path is not defined. Your route definition of
@api.route('/', methods=["GET", "POST"])
def home():
response = make_response(jsonify('Success'), 200)
response.headers["Content-type"] = "application/json"
return response
basically translates to the route /test/. Rather what you should be verifying against is the path /test/
Hi, @onlinejudge95 thanks for the response but even if I change base_url parameter to '/' while defining API, then also below view gives a 404 error code.
@api.route('/', methods=["GET", "POST"])
def home():
response = make_response(jsonify('Success'), 200)
response.headers["Content-type"] = "application/json"
return response
I suggest you do a flask routes first
Yeah, that's what I'm doing right now. I was hoping for a workaround to that.
Thanks
Any news on this? We face the same issue. Even when we force swagger docs to a dedicated path or set add_specs=False calling root path "/" will return 404 even though there are matching route handlers registered.
@SebastianSulzbacherEWE It's mentioned briefly in the quick start docs. Due to how flask-restx builds URLs, the Api object always creates the / route if it doesn't exist when the Api is instantiated. If you move the docs to another path, it still registers the root path and explicitly returns 404. There is no easy fix to this behaviour inside flask-restx, unfortunately.
However, you can still use the / route, you just need to register it in Flask before you call Api in flask-restx. See this comment for an example of how to work around this.