flask-restx icon indicating copy to clipboard operation
flask-restx copied to clipboard

Indicate Base URL in Swagger UI

Open khushhalm opened this issue 5 years ago • 6 comments

***** 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 -

khushhalm avatar May 20 '20 18:05 khushhalm

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/

onlinejudge95 avatar May 29 '20 18:05 onlinejudge95

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

khushhalm avatar Jun 05 '20 09:06 khushhalm

I suggest you do a flask routes first

onlinejudge95 avatar Jun 05 '20 10:06 onlinejudge95

Yeah, that's what I'm doing right now. I was hoping for a workaround to that.

Thanks

khushhalm avatar Jun 05 '20 10:06 khushhalm

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 avatar Apr 11 '23 09:04 SebastianSulzbacherEWE

@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.

peter-doggart avatar Apr 11 '23 09:04 peter-doggart