Swagger UI blank after overriding host

1,000 views
Skip to first unread message

Brian Sewell

unread,
Jun 9, 2016, 4:50:34 PM6/9/16
to Swagger
Info: Scala API with Play Framework
Scala Version: 2.11.7
Library: io.swagger swagger-play2 version 1.51

Original issue: 
working with an API that users connect to using a reverse proxy.

Ex. base url: https://myapp.com

Ex. proxy url: https://userapp.com

I'm trying to find out how to adjust the Request URL in the Swagger output to be the proxy and not the base.

I updated my index.html page to override the host, but now my swagger ui does not find the json docs and is blank. Any advice/insights?

 

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Swagger UI</title>
  <link rel="icon" type="image/png" href="images/favicon-32x32.png" sizes="32x32" />
  <link rel="icon" type="image/png" href="images/favicon-16x16.png" sizes="16x16" />
  <link href='css/typography.css' media='screen' rel='stylesheet' type='text/css'/>
  <link href='css/reset.css' media='screen' rel='stylesheet' type='text/css'/>
  <link href='css/screen.css' media='screen' rel='stylesheet' type='text/css'/>
  <link href='css/reset.css' media='print' rel='stylesheet' type='text/css'/>
  <link href='css/print.css' media='print' rel='stylesheet' type='text/css'/>
  <script src='lib/jquery-1.8.0.min.js' type='text/javascript'></script>
  <script src='lib/jquery.slideto.min.js' type='text/javascript'></script>
  <script src='lib/jquery.wiggle.min.js' type='text/javascript'></script>
  <script src='lib/jquery.ba-bbq.min.js' type='text/javascript'></script>
  <script src='lib/handlebars-2.0.0.js' type='text/javascript'></script>
  <script src='lib/js-yaml.min.js' type='text/javascript'></script>
  <script src='lib/lodash.min.js' type='text/javascript'></script>
  <script src='lib/backbone-min.js' type='text/javascript'></script>
  <script src='lib/underscore-min.js' type='text/javascript'></script>
  <script src='swagger-ui.js' type='text/javascript'></script>
  <script src='lib/highlight.9.1.0.pack.js' type='text/javascript'></script>
  <script src='lib/highlight.9.1.0.pack_extended.js' type='text/javascript'></script>
  <script src='lib/jsoneditor.min.js' type='text/javascript'></script>
  <script src='lib/marked.js' type='text/javascript'></script>
  <script src='lib/swagger-oauth.js' type='text/javascript'></script>

  <!-- Some basic translations -->
  <!-- <script src='lang/translator.js' type='text/javascript'></script> -->
  <!-- <script src='lang/ru.js' type='text/javascript'></script> -->
  <!-- <script src='lang/en.js' type='text/javascript'></script> -->

  <script type="text/javascript">
    $(function () {
      var url = window.location.origin + "/puppies";
      if (url && url.length > 1) {
        url = decodeURIComponent(url)+"/api-docs.json";
      } else {
      }

      hljs.configure({
        highlightSizeThreshold: 5000
      });

      // Pre load translate...
      if(window.SwaggerTranslator) {
        window.SwaggerTranslator.translate();
      }
      window.swaggerUi = new SwaggerUi({
        url: url,
        dom_id: "swagger-ui-container",
        supportedSubmitMethods: ['get', 'post', 'put', 'delete', 'patch'],
        onComplete: function(swaggerApi, swaggerUi){
swaggerUi.api.setHost(“myapp.com");
          if(typeof initOAuth == "function") {
            initOAuth({
              clientId: "your-client-id",
              clientSecret: "your-client-secret-if-required",
              realm: "your-realms",
              appName: "your-app-name",
              scopeSeparator: ",",
              additionalQueryStringParams: {}
            });
          }

          if(window.SwaggerTranslator) {
            window.SwaggerTranslator.translate();
          }
        },
        onFailure: function(data) {
          log("Unable to Load SwaggerUI");
        },
        docExpansion: "none",
        jsonEditor: false,
        defaultModelRendering: 'schema',
        showRequestHeaders: false
      });

      window.swaggerUi.load();

      function log() {
        if ('console' in window) {
          console.log.apply(console, arguments);
        }
      }
  });
  </script>
</head>

<body class="swagger-section">
<div id='header'>
  <div class="swagger-ui-wrap">
    <a id="logo" href="http://swagger.io"><img class="logo__img" alt="swagger" height="30" width="30" src="images/logo_small.png" /><span class="logo__title">swagger</span></a>
    <form id='api_selector'>
      <div class='input'><input placeholder="http://example.com/api" id="input_baseUrl" name="baseUrl" type="text"/></div>
      <div id='auth_container'></div>
      <div class='input'><a id="explore" class="header__btn" href="#" data-sw-translate>Explore</a></div>
    </form>
  </div>
</div>

<div id="message-bar" class="swagger-ui-wrap" data-sw-translate>&nbsp;</div>
<div id="swagger-ui-container" class="swagger-ui-wrap"></div>
</body>
</html>

Ron Ratovsky

unread,
Jun 9, 2016, 5:00:09 PM6/9/16
to swagger-sw...@googlegroups.com

Can you check your browser’s console for any errors?

--
You received this message because you are subscribed to the Google Groups "Swagger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Brian Sewell

unread,
Jun 9, 2016, 5:09:23 PM6/9/16
to Swagger
Hi Ron-

It appears that the swaggerUi.api.setHost on line 50 is giving a swagger:50 Uncaught SyntaxError: Unexpected token ILLEGAL error.

Where would be the correct place to put this?

Regards,

Brian

To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggersocket+unsub...@googlegroups.com.

Ron Ratovsky

unread,
Jun 9, 2016, 5:17:57 PM6/9/16
to swagger-sw...@googlegroups.com

Normally after swaggerUi has been initialized with the spec.

To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.


For more options, visit https://groups.google.com/d/optout.

--

You received this message because you are subscribed to the Google Groups "Swagger" group.

To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.

Brian Sewell

unread,
Jun 9, 2016, 5:28:10 PM6/9/16
to Swagger
Perfect! I got it to work.

Quick question, is there a way to do it dynamically? I.e if I have multiple environments,

Ron Ratovsky

unread,
Jun 9, 2016, 5:35:48 PM6/9/16
to swagger-sw...@googlegroups.com

With some basic javascript/html you can do anything. Just create a drop down with the options for the user to choose and have a call to update the host based on the chosen value.

 

 

 

From: <swagger-sw...@googlegroups.com> on behalf of Brian Sewell <brianse...@gmail.com>
Reply-To: "swagger-sw...@googlegroups.com" <swagger-sw...@googlegroups.com>
Date: Thursday, 9 June 2016 at 14:28
To: Swagger <swagger-sw...@googlegroups.com>
Subject: Re: Swagger UI blank after overriding host

 

Perfect! I got it to work.

--

Brian Sewell

unread,
Jun 9, 2016, 5:48:34 PM6/9/16
to Swagger
I'm not actually too familiar with javascipt/html, this is my first attempt. Could you provide an example?

To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggersocket+unsub...@googlegroups.com.

Brian Sewell

unread,
Jun 9, 2016, 5:49:16 PM6/9/16
to Swagger
I was hoping to have a way to read the current host and search for the string pst or tst

Brian Sewell

unread,
Jun 9, 2016, 6:08:24 PM6/9/16
to Swagger
I'm trying to use the code below.

if(window.host == "https://myapp-tst.com"){
          swaggerUi.api.setHost(" https://userapp-tst.com);}
          else {swaggerUi.api.setHost(" https://userapp.com);};

What would the correct var/val for host be though?

Thanks,

Brian
Reply all
Reply to author
Forward
0 new messages