Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwaggerUI generates a port number when making the request to the API. Is there any way to stop this behaviour?

I have a Web API hosted on Pivotal Cloud foundry; which includes the Swagger Documentation for it.

However whenever I am trying to test any API endpoint, Swagger puts in the request URL a port number :port after the host, and this is preventing the connection to be made to the API endpoint and thus no data is returned. For some reason, APIs hosted on pivotal are not comfortable with port number; instead just the route url seems to work fine.

I am hosting a ASP.NET Web API and using Swashbuckle Swagger Nuget Package for generating the documentation.

Is there any way to force Swagger to remove the port number when it sends the api request?

like image 339
sohamangoes Avatar asked Aug 16 '17 07:08

sohamangoes


2 Answers

May be this will help. The answer from mknopf is correct. I couldn't upvote due to limitation with reputation points. However I am drafting my code here for others to quickly solve if they encounter this problem.

In the SwaggerConfig.cs file (only if you have the configuration set from a source file rather than a config file itself) update the RoolUrl statement as below:

c.RootUrl(req => ComputeHostAsSeenByOriginalClient(req));

Then add the below method in your swaggerconfig.cs file:

private static string ComputeHostAsSeenByOriginalClient(HttpRequestMessage req)
{
   var authority = req.Headers.Host;
   var scheme = req.RequestUri.Scheme;
   if (req.Headers.Contains("X-Forwarded-Host"))
   {
       var xForwardedHost = req.Headers.GetValues("X-Forwarded-Host").First();
       var firstForwardedHost = xForwardedHost.Split(',')[0];
       authority = firstForwardedHost;
   }
   if (req.Headers.Contains("X-Forwarded-Proto"))
   {
       var xForwardedProto = req.Headers.GetValues("X-Forwarded-Proto").First();
       if (xForwardedProto.IndexOf(",") != -1)
       {
          //when multiple apache, X-Forwarded-Proto is also multiple ...
          xForwardedProto = xForwardedProto.Split(',')[0];
       }
       scheme = xForwardedProto;
   }
   return scheme + "://" + authority;
}

If you still have problems with the root url, I suggest to debug the req.Headers by logging the values and review which header member to be used for "authority" variable.

like image 170
Jabez Avatar answered Sep 29 '22 09:09

Jabez


I know this is a rather late reply but i'm adding it so that others whom run into this in the future hopefully can find a resolution quickly.

I ran into this exact same problem. I found this documentation on the Swashbuckle Git repo:

By default, the service root url is inferred from the request used to access the docs. However, there may be situations (e.g. proxy and load-balanced environments) where this does not resolve correctly. You can workaround this by providing your own code to determine the root URL.

Inside your App_Start/SwaggerConfig.cs file you need to un-comment the line for c.RootUrl(req => GetRootUrlFromAppConfig()) and implement the method GetRootUrlFromAppConfig() so that it returns the proper root url for your application (see the screenshot below)

modify the SwaggerConfig.cs file

And here are some examples of implementation for the GetRootUrlFromAppConfig(), you will need to determine which one best suites your specific situation: https://github.com/domaindrivendev/Swashbuckle/issues/705

like image 33
mknopf Avatar answered Sep 29 '22 08:09

mknopf