Custom Header Issue

24 views
Skip to first unread message

baskar ia

unread,
May 25, 2016, 4:06:55 PM5/25/16
to Swagger
I'm trying to add the custom header in all my request. Below is the script from index.html. I know i'm doing something wrong Please help me in this.



<script type="text/javascript">
    $(function () {
      var url = window.location.search.match(/url=([^&]+)/);
      if (url && url.length > 1) {
        url = decodeURIComponent(url[1]);
      } else {
        url = "/myproject/rest/swagger.json";
      }
      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){
          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();
          }
          addApiKeyAuthorization();
        },
        onFailure: function(data) {
          log("Unable to Load SwaggerUI");
        },
        docExpansion: "none",
        jsonEditor: false,
        defaultModelRendering: 'schema',
        showRequestHeaders: true
      });
      
      function addApiKeyAuthorization(){
         var key = encodeURIComponent($('#input_apiKey')[0].value);
         if(key && key.trim() != "") {
             swaggerUi.api.clientAuthorizations.add("Auth", new SwaggerClient.ApiKeyAuthorization("Auth", key, "header"));
             swaggerUi.api.clientAuthorizations.add("HEAD1", new SwaggerClient.ApiKeyAuthorization("HEAD1", "TEST1", "header"));
             swaggerUi.api.clientAuthorizations.add("HEAD2", new SwaggerClient.ApiKeyAuthorization("HEAD2", "TEST2", "header"));
             log("added key " + key);
         }
       }      
       
      window.swaggerUi.load();
      function log() {
        if ('console' in window) {
          console.log.apply(console, arguments);
        }
      }
  });
  </script>

tony tam

unread,
May 26, 2016, 9:14:30 AM5/26/16
to swagger-sw...@googlegroups.com
Try setting 

var key = “some-value”;

and reloading the page.  You may need your server to respond to the options call with Access-Control-Allow-Headers with those header names.
--
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.

Reply all
Reply to author
Forward
0 new messages