Why is there "no response from server" in Swagger UI?

Viewed 15604

My API calls work correctly in Postman. But when I send requests from Swagger UI, it shows "no response from server" for all requests:

Response Body
no content

Response Code
0

Response Headers

{
  "error": "no response from server"
}

Swagger UI - no response from server

What can the problem be and how to fix it?

The browser console shows these errors:

Failed to load resource: net::ERR_CONNECTION_REFUSED

Uncaught TypeError: Cannot read property 'length' of undefined
   at showStatus (index.js:24)
   at showErrorStatus (index.js:24)
   at error (index.js:607) at spec-converter.js:533
   at Request.callback (index.js:24)
   at Request.crossDomainError (index.js:24)
   at XMLHttpRequest.xhr.onreadystatechange (index.js:24)

Swagger UI - console errors

4 Answers

Swagger returns 0 response code due to the Reference Looping in your serializer response.

Ignore the reference looping while getting the serializer response back.

If you are using Web API, use the following code

services.AddMvc()
                .AddJsonOptions(opt =>
                { 
                  opt.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
                });

remove schemes from swagger config json file(in my case tsoa.json) and restart the server. It worked for me.

{
    "readme": "https://github.com/lukeautry/tsoa/blob/HEAD/tsoa.json",
    "swagger": {
        "outputDirectory": "./dist/src",
        "entryFile": "./src/server.ts",
        "basePath": "/",
        "schemes": [
            "http",
            "https"
          ],
        "securityDefinitions": {
            "basic": {
                "type": "basic"
            }
        }
    },
    "routes": {
        "basePath": "/",
        "entryFile": "./src/server.ts",
        "routesDir": "./src",
        "authenticationModule": "./src/security/Authentication"
    }
}

In my case there was a problem with https. In swagger config the http scheme was "disabled" (not available). I had it like this:

 GlobalConfiguration.Configuration.EnableSwagger(c => { c.Schemes(new[] { "https" }); });

And I had to change it to make it work in debug on localhost:

GlobalConfiguration.Configuration.EnableSwagger(c => { c.Schemes(new[] { "https", "http" }); });

The first snippet worked on production with enabled https but id didn't work while debugging in Visual Studio in default config.

Related