question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to solve failed to load API definition?

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
sveintcommented, Mar 25, 2020

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.

1reaction
sveintcommented, Mar 6, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Swashbuckle/Swagger + ASP.Net Core: "Failed to load API ...
This helped me find the answer. The problem was a public helper method in a controller. Changed it to protected - swagger file...
Read more >
WebApi Core / Swagger: "failed to load API definition" error
I was working with some ASP.NET WebApi code and needed to test something using the Swagger UI. Usually, it's just a matter of...
Read more >
Failed to load Swagger API definition for a Newly Published ...
I just pushed a .NET Core 6.0 WebAPI to Azure AppService. When I access the location I receive the following error issued when...
Read more >
Resolved:Failed to load API definition (undefined /swagger/v1 ...
Please make sure all controller methods are attributed with proper HTTP attributes Example- HttpGET or HttpPost etc. Missing this attribute ...
Read more >
Swagger Failed to load API definition In ASP.NET Core API
The solution is that you have to change the default route configuration at the controller level when you have created more than one...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found