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

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

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 fortable 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?

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 fortable 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?

Share Improve this question edited Aug 16, 2017 at 8:24 sohamangoes asked Aug 16, 2017 at 7:45 sohamangoessohamangoes 1292 silver badges7 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

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.

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-ment 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)

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745387612a4625509.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信