N Sharma
N Sharma

Reputation: 34487

Swagger ui is not loading swagger api json

I have made api documention with the help of swagger-editor. Now I want see it graphically with the help of swagger-ui. I have deployed my json here https://powerful-escarpment-92284.herokuapp.com/swagger.json

Now When I am trying to access it then I am getting an error

Failed to load spec.

enter image description here

Upvotes: 2

Views: 6414

Answers (4)

Javad Alizadeh
Javad Alizadeh

Reputation: 105

The URL is not shown, so I assume if you access the swagger directly via "index.html" or any other way, the URL format must be like this.

http://<host>:<port>/<project_name>/_swagger-ui/index.html?url=http://<host>:<port>/<project_name>/<path>/swagger.json

like: http://localhost:8080/counter/_swagger-ui/index.html?url=http://localhost:8080/counter/webapi/swagger.json

I hope, it helps :)

Upvotes: 0

Kirsten
Kirsten

Reputation: 18076

I was getting this error in a c# project, due to a problem with a controller I added. I could get more information by trying to browse to the swagger.json It turned out I was missing the routing attribute on a controller action.

Upvotes: 1

Datalker
Datalker

Reputation: 356

After many exercises for me apparently, it helped just to serve a file with flask server [How to serve static files in Flask. and (important) add "?" to the end of the url, i.e. http://127.0.0.1/js/swagger.json?

Upvotes: 0

N Sharma
N Sharma

Reputation: 34487

I solved this by setting this header in the response of swagger.json file.

app.get('/:file(*)', function(req, res, next){
    var file = req.params.file, path = __dirname + '/' + file;

    console.log('.');
    res.header('Access-Control-Allow-Origin', '*');

    res.download(path);
});

Upvotes: 2

Related Questions