diff --git a/README.md b/README.md index a3d590de1..e5db8b023 100644 --- a/README.md +++ b/README.md @@ -224,6 +224,46 @@ You can add it to the swagger-client like such: client.clientAuthorizations.add('my-auth', new CustomRequestSigner()); ``` +### Setting headers + +Headers are a type of `parameter`, and can be passed with the other parameters. For example, if you supported translated pet details via the `Accept-Language` header: + +```js +"parameters": [ + { + "name": "petId", + "description": "ID of pet that needs to be fetched", + "required": true, + "type": "integer", + "format": "int64", + "paramType": "path", + "minimum": "1.0", + "defaultValue": 3, + "maximum": "100000.0" + }, + "LanguageHeader": { + "name": "Accept-Language", + "in": "header", + "description": "Specify the user's language", + "required": false, + "type": "string" + } +... +``` + +Then you would pass the header value via the parameters ([header parameters are case-insenstive](https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2)): + +```js + +client.pet.getPetById({ + petId: 7, + 'accept-language': 'fr' +}, function(pet){ + console.log('pet', pet); +}); + +``` + ### Using your own HTTP client Don't like [superagent](https://github.com/visionmedia/superagent)? Despise [JQuery](https://github.com/jquery/jquery)? Well, you're in luck. You can plug your own HTTP library easily: diff --git a/lib/types/operation.js b/lib/types/operation.js index 036b12ba2..2247e40ff 100644 --- a/lib/types/operation.js +++ b/lib/types/operation.js @@ -431,20 +431,26 @@ Operation.prototype.supportedSubmitMethods = function () { Operation.prototype.getHeaderParams = function (args) { var headers = this.setContentTypes(args, {}); + var headerParamsByLowerCase = {}; for (var i = 0; i < this.parameters.length; i++) { var param = this.parameters[i]; - if (typeof args[param.name] !== 'undefined') { - if (param.in === 'header') { - var value = args[param.name]; + if (param.in === 'header') { + headerParamsByLowerCase[param.name.toLowerCase()] = param; + } + } - if (Array.isArray(value)) { - value = value.toString(); - } + for (var arg in args) { + var headerParam = headerParamsByLowerCase[arg.toLowerCase()]; + if (typeof headerParam !== 'undefined') { + var value = args[arg]; - headers[param.name] = value; + if (Array.isArray(value)) { + value = value.toString(); } + + headers[headerParam.name] = value; } } diff --git a/test/client.js b/test/client.js index 209ae7f6f..b30f5f6b4 100644 --- a/test/client.js +++ b/test/client.js @@ -699,7 +699,7 @@ describe('SwaggerClient', function () { parameters: [ { in: 'header', - name: 'username', + name: 'UserNaMe', type: 'string' } ], @@ -731,7 +731,7 @@ describe('SwaggerClient', function () { **/ // ensure the headers are present - expect(requestObj.headers.username).toBe('bob'); + expect(requestObj.headers.UserNaMe).toBe('Bob'); // rewrite this request to something that'll work locally requestObj.method = 'GET'; @@ -747,7 +747,7 @@ describe('SwaggerClient', function () { usePromise: true, requestInterceptor: interceptor.requestInterceptor }).then(function(client) { - client.nada.addFoo({username: 'bob'}).then(function (){ + client.nada.addFoo({Username: 'Bob'}).then(function (){ done(); }); }).catch(function(exception) { diff --git a/test/headers.js b/test/headers.js index 0a0069437..f1e4456b7 100644 --- a/test/headers.js +++ b/test/headers.js @@ -25,6 +25,24 @@ describe('header extraction', function () { expect(headers.myHeader).toBe('tony'); }); + it('should extract header params with case insensitivity', function () { + var parameters = [ + { + in: 'header', + name: 'myHeader', + type: 'string' + } + ]; + var op = new Operation({}, 'http', 'test', 'get', '/path', { parameters: parameters }); + var args = { + MyHeAdeR: 'tony' + }; + var url = op.urlify(args); + var headers = op.getHeaderParams(args); + + expect(url).toBe('http://localhost/path'); + expect(headers.myHeader).toBe('tony'); + }); it('should not URL encode header string values', function () { var parameters = [ diff --git a/test/spec/v2/spec3.json b/test/spec/v2/spec3.json index b652beeb6..a92b766c8 100644 --- a/test/spec/v2/spec3.json +++ b/test/spec/v2/spec3.json @@ -25,6 +25,9 @@ "description": "Status values that need to be considered for filter", "required": false, "type": "string" + }, + { + "$ref": "#/parameters/LanguageHeader" } ], "responses": { @@ -40,5 +43,14 @@ } } } + }, + "parameters": { + "LanguageHeader": { + "name": "Accept-Language", + "in": "header", + "description": "Specify the user's language", + "required": false, + "type": "string" + } } -} \ No newline at end of file +}