|
259 | 259 | return headers; |
260 | 260 | } |
261 | 261 |
|
| 262 | + /** |
| 263 | + * Retrieve body request with valid format depending content type |
| 264 | + * |
| 265 | + * @param {Mixed} data |
| 266 | + * @param {String} contentType |
| 267 | + * @return {Mixed} |
| 268 | + */ |
| 269 | + function getBodyRequest(data, contentType) { |
| 270 | + if (contentType === 'application/x-www-form-urlencoded' && typeof data === 'object') { |
| 271 | + return uriEncode(data); |
| 272 | + } |
| 273 | + return data; |
| 274 | + } |
| 275 | + |
262 | 276 | /** |
263 | 277 | * Sanitize the scopes option to be a string. |
264 | 278 | * |
|
356 | 370 | ClientOAuth2.prototype.request = function (options, done) { |
357 | 371 | var xhr = new root.XMLHttpRequest(); |
358 | 372 | var headers = options.headers || {}; |
| 373 | + var body = getBodyRequest(options.data, options.contentType); |
359 | 374 |
|
360 | 375 | // Open the request to the url and method. |
361 | 376 | xhr.open(options.method, options.url); |
|
381 | 396 | }); |
382 | 397 |
|
383 | 398 | // Make the request with the body. |
384 | | - xhr.send(options.body); |
| 399 | + xhr.send(body); |
385 | 400 | }; |
386 | 401 | } else { |
387 | 402 | var url = require('url'); |
|
531 | 546 | 'Content-Type': 'application/x-www-form-urlencoded', |
532 | 547 | 'Authorization': 'Basic ' + authorization |
533 | 548 | }, |
534 | | - body: uriEncode({ |
| 549 | + data: uriEncode({ |
535 | 550 | refresh_token: this.refreshToken, |
536 | 551 | grant_type: 'refresh_token' |
537 | 552 | }) |
|
593 | 608 | 'Content-Type': 'application/x-www-form-urlencoded', |
594 | 609 | 'Authorization': 'Basic ' + authorization |
595 | 610 | }, |
596 | | - body: uriEncode({ |
| 611 | + data: uriEncode({ |
597 | 612 | scope: sanitizeScope(options.scopes), |
598 | 613 | username: username, |
599 | 614 | password: password, |
|
743 | 758 | 'Content-Type': 'application/x-www-form-urlencoded', |
744 | 759 | 'Authorization': 'Basic ' + authorization |
745 | 760 | }, |
746 | | - body: uriEncode({ |
| 761 | + data: uriEncode({ |
747 | 762 | scope: sanitizeScope(options.scopes), |
748 | 763 | grant_type: 'client_credentials' |
749 | 764 | }) |
|
855 | 870 | 'Accept': 'application/json, application/x-www-form-urlencoded', |
856 | 871 | 'Content-Type': 'application/x-www-form-urlencoded' |
857 | 872 | }, |
858 | | - body: uriEncode({ |
| 873 | + data: uriEncode({ |
859 | 874 | code: query.code, |
860 | 875 | grant_type: 'authorization_code', |
861 | 876 | redirect_uri: options.redirectUri, |
|
0 commit comments