diff --git a/README.md b/README.md index 05f58edb..58744490 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,7 @@ To use swagger-ui you should take a look at the [source of swagger-ui html page] dom_id:"swagger-ui-container", apiKey:"special-key", supportHeaderParams: false, + headers: { "Authorization": "XXXX", "someOtherHeader": "YYYY" }, supportedSubmitMethods: ['get', 'post', 'put'] }); @@ -71,6 +72,11 @@ header parameters are supported. However because of [Cross-Origin Resource Shari supportHeaderParams: true +### Custom Header Parameters - (For Basic auth etc) +If you have some header parameters which you need to send with every request, use the headers as below: + + headers: { "Authorization": "XXXX", "someOtherHeader": "YYYY" } + ### Api Key Parameter If you enter an api key in swagger-ui, it sends a parameter named 'api\_key' as a query (or as a header param if you've enabled it as described above). You may not want to use the name 'api\_key' as the name of this parameter. You can change its name by setting the _apiKeyName_ parameter when you instantiate a SwaggerUI instance. For example to call it 'sessionId' diff --git a/lib/swagger.js b/lib/swagger.js index 406a48cc..37d33ce3 100644 --- a/lib/swagger.js +++ b/lib/swagger.js @@ -40,6 +40,7 @@ } this.failure = options.failure != null ? options.failure : function() {}; this.progress = options.progress != null ? options.progress : function() {}; + this.headers = options.headers != null ? options.headers : {}; this.discoveryUrl = this.suffixApiKey(this.discoveryUrl); if (options.success != null) { this.build(); @@ -394,7 +395,7 @@ }; SwaggerOperation.prototype.getMatchingParams = function(paramTypes, args, includeApiKey) { - var matchingParams, param, _i, _len, _ref; + var matchingParams, name, param, value, _i, _len, _ref, _ref1; matchingParams = {}; _ref = this.parameters; for (_i = 0, _len = _ref.length; _i < _len; _i++) { @@ -406,6 +407,13 @@ if (includeApiKey && (this.resource.api.api_key != null) && this.resource.api.api_key.length > 0) { matchingParams[this.resource.api.apiKeyName] = this.resource.api.api_key; } + if (jQuery.inArray('header', paramTypes) >= 0) { + _ref1 = this.resource.api.headers; + for (name in _ref1) { + value = _ref1[name]; + matchingParams[name] = value; + } + } return matchingParams; };