Skip to content

Commit 058b2ac

Browse files
authored
Merge pull request #865 from cesine/feature/case-insensitive-headers
Adding docs and examples for headers
2 parents 17495a5 + 60388a3 commit 058b2ac

File tree

4 files changed

+59
-7
lines changed

4 files changed

+59
-7
lines changed

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,46 @@ You can add it to the swagger-client like such:
225225
client.clientAuthorizations.add('my-auth', new CustomRequestSigner());
226226
```
227227

228+
### Setting headers
229+
230+
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:
231+
232+
```js
233+
"parameters": [
234+
{
235+
"name": "petId",
236+
"description": "ID of pet that needs to be fetched",
237+
"required": true,
238+
"type": "integer",
239+
"format": "int64",
240+
"paramType": "path",
241+
"minimum": "1.0",
242+
"defaultValue": 3,
243+
"maximum": "100000.0"
244+
},
245+
"LanguageHeader": {
246+
"name": "Accept-Language",
247+
"in": "header",
248+
"description": "Specify the user's language",
249+
"required": false,
250+
"type": "string"
251+
}
252+
...
253+
```
254+
255+
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)):
256+
257+
```js
258+
259+
client.pet.getPetById({
260+
petId: 7,
261+
'accept-language': 'fr'
262+
}, function(pet){
263+
console.log('pet', pet);
264+
});
265+
266+
```
267+
228268
### Using your own HTTP client
229269

230270
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:

test/client.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ describe('SwaggerClient', function () {
703703
parameters: [
704704
{
705705
in: 'header',
706-
name: 'username',
706+
name: 'Accept-Language',
707707
type: 'string'
708708
}
709709
],
@@ -735,7 +735,7 @@ describe('SwaggerClient', function () {
735735
**/
736736

737737
// ensure the headers are present
738-
expect(requestObj.headers.username).toBe('bob');
738+
expect(requestObj.headers['Accept-Language']).toBe('fr');
739739

740740
// rewrite this request to something that'll work locally
741741
requestObj.method = 'GET';
@@ -751,7 +751,7 @@ describe('SwaggerClient', function () {
751751
usePromise: true,
752752
requestInterceptor: interceptor.requestInterceptor
753753
}).then(function(client) {
754-
client.nada.addFoo({username: 'bob'}).then(function (){
754+
client.nada.addFoo({'accept-LANGUAGE': 'fr'}).then(function (){
755755
done();
756756
});
757757
}).catch(function(exception) {

test/headers.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,19 @@ describe('header extraction', function () {
2929
var parameters = [
3030
{
3131
in: 'header',
32-
name: 'myHeader',
32+
name: 'Request-ID',
3333
type: 'string'
3434
}
3535
];
3636
var op = new Operation({}, 'http', 'test', 'get', '/path', { parameters: parameters });
3737
var args = {
38-
MyHeAdeR: 'nick'
38+
'request-Id': '12ab34ef'
3939
};
4040
var url = op.urlify(args);
4141
var headers = op.getHeaderParams(args);
4242

4343
expect(url).toBe('http://localhost/path');
44-
expect(headers.myHeader).toBe('nick');
44+
expect(headers['Request-ID']).toBe('12ab34ef');
4545
});
4646

4747
it('should not URL encode header string values', function () {

test/spec/v2/spec3.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
"description": "Status values that need to be considered for filter",
2626
"required": false,
2727
"type": "string"
28+
},
29+
{
30+
"$ref": "#/parameters/LanguageHeader"
2831
}
2932
],
3033
"responses": {
@@ -40,5 +43,14 @@
4043
}
4144
}
4245
}
46+
},
47+
"parameters": {
48+
"LanguageHeader": {
49+
"name": "Accept-Language",
50+
"in": "header",
51+
"description": "Specify the user's language",
52+
"required": false,
53+
"type": "string"
54+
}
4355
}
44-
}
56+
}

0 commit comments

Comments
 (0)