Can the base URL change in Swagger?

15,167

Solution 1

This is possible, and I implement this the following way:

In swaggerui, I programmatically declare my url by fetching the url via javascript:

  // Get the url:
  theUrl = window.location.protocol+"//" + window.location.host+"/docs";

  window.swaggerUi = new SwaggerUi({
  url: theUrl,..other parameters...})

In my json files, where I declare a resource, I just declare my basepath as a "/" as shown below:

{
  "apiVersion": "1.0.0",
  "swaggerVersion": "1.2",
  "basePath": "/",
  "resourcePath": "/api/myapi".......

Hope that helps!

Solution 2

To change dynamically the hostname (and override the value from JSON file) of the targeted server (where the REST request is sent) :

$.each(window.swaggerUi.api.apis, function(key, val) {
  window.swaggerUi.api.apis[key].basePath = "http://target:port";
});
Share:
15,167
Admin
Author by

Admin

Updated on August 19, 2022

Comments

  • Admin
    Admin over 1 year

    I currently have implemented Swagger and I've noticed that the base url for resources is hardcoded in the JSON resource files, ideally I would like to give the user the capability to change the base url for different json verbages. So, for example, give them the capability to submit a get from one environment and a put from another on the same page since I'm working with multiple environments and otherwise they'd have to alter the JSON in every one of their resources each time they want to use a new environment. Does anyone know if this is possible?

  • Mop So
    Mop So over 6 years
    It's javascript code to execute in Swagger page to change all base pathes