flask-swagger-ui
flask-swagger-ui copied to clipboard
How to solve failed to load API definition?
I am trying to configure the swagger.json but every time it is giving the below error.
Fetch errorFailed to fetch C:\Project\test\swagger.json
Fetch errorPossible cross-origin (CORS) issue? The URL origin (file://) does not match the page (http://127.0.0.1:5000). Check the server returns the correct 'Access-Control-Allow-*' headers.
When i clicked on "Invalid" button {"schemaValidationMessages":[{"level":"error","message":"Can't read from file c:/Project\test\\swagger.json"}]}
this error is coming .
SWAGGER_URL = '/swagger'
API_URL = 'C:\Project\test\swagger.json'
SWAGGERUI_BLUEPRINT = get_swaggerui_blueprint(
SWAGGER_URL,
API_URL,
config={
'app_name': "Seans-Python-Flask-REST-Boilerplate"
}
)
app.register_blueprint(SWAGGERUI_BLUEPRINT, url_prefix=SWAGGER_URL)
Let me know what i am doing wrong.
Hi,
the API_URL should be a endpoint (resource) at your server (e.g. /docs/swagger.json), not a file on the system. In other words you'll need to manually serve this file somehow at a given resource.
Would you happen to have an example of this? I am also running into this problem and unsure how to resolve it.
You could do something like this modified example, if using flask to serve the file:
import os
from flask import Flask, send_from_directory
from flask_swagger_ui import get_swaggerui_blueprint
app = Flask(__name__)
SWAGGER_URL = "/api/docs"
API_URL = "/docs/swagger.json"
swaggerui_blueprint = get_swaggerui_blueprint(
SWAGGER_URL,
API_URL,
config={"app_name": "Test application"}
)
app.register_blueprint(swaggerui_blueprint, url_prefix=SWAGGER_URL)
@app.route("/docs/swagger.json")
def specs():
return send_from_directory(os.getcwd(), "swagger.json")
app.run()
This assumes that swagger.json
is in current working directory, so adjust accordingly.
ok, that seems to help a bit.
I've still got a blank swagger.json though, how do I generate the documentation?
https://swagger.io/docs/specification/basic-structure/