Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
20 changes: 13 additions & 7 deletions lib/types/operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
6 changes: 3 additions & 3 deletions test/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ describe('SwaggerClient', function () {
parameters: [
{
in: 'header',
name: 'username',
name: 'UserNaMe',
type: 'string'
}
],
Expand Down Expand Up @@ -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';
Expand All @@ -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) {
Expand Down
18 changes: 18 additions & 0 deletions test/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
14 changes: 13 additions & 1 deletion test/spec/v2/spec3.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
"description": "Status values that need to be considered for filter",
"required": false,
"type": "string"
},
{
"$ref": "#/parameters/LanguageHeader"
}
],
"responses": {
Expand All @@ -40,5 +43,14 @@
}
}
}
},
"parameters": {
"LanguageHeader": {
"name": "Accept-Language",
"in": "header",
"description": "Specify the user's language",
"required": false,
"type": "string"
}
}
}
}