Skip to content

Commit 60388a3

Browse files
committed
adding docs and examples for headers
1 parent 20bac9e commit 60388a3

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
@@ -224,6 +224,46 @@ You can add it to the swagger-client like such:
224224
client.clientAuthorizations.add('my-auth', new CustomRequestSigner());
225225
```
226226

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

229269
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)