Swagger Multiple hosts in same Json spec

2019-02-18 07:08发布

问题:

I am using a single host for documenting REST API's in Swagger Ui 2.0 but I need two hosts in the JSON file for calling rest API's one for http and the other one for https. Is it possible? If yes then how to do that?

Thanks!

回答1:

The way swagger figures out URLs is this:

  1. You provide the basic one in index.html from where the swagger.json gets generated. The generated swagger.json does not contain a URL per se, or any http/https information. It only has a path relative to the base URL you provided.
  2. After the UI gets generated based on generated swagger.json, the "Try it out" buttons execute GET/POST/PUT requests based on the URL info in the address bar. check this piece of code in your swagger-ui.js:

    if (url && url.indexOf('http') !== 0) { url = this.buildUrl(window.location.href.toString(), url); }

So, if you want to use https, use https in the address bar to hit Swagger UI. You will also need to mention the same in your index.html, and in swagger-ui.js in the above code.