Skip to content

Commit ca9cae1

Browse files
committed
Merge pull request #155 from mons54/master
fix body request [issue #133]
2 parents 5947a45 + 2c90630 commit ca9cae1

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

src/vendor/client-oauth2/client-oauth2.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,20 @@
259259
return headers;
260260
}
261261

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+
262276
/**
263277
* Sanitize the scopes option to be a string.
264278
*
@@ -356,6 +370,7 @@
356370
ClientOAuth2.prototype.request = function (options, done) {
357371
var xhr = new root.XMLHttpRequest();
358372
var headers = options.headers || {};
373+
var body = getBodyRequest(options.data, options.contentType);
359374

360375
// Open the request to the url and method.
361376
xhr.open(options.method, options.url);
@@ -381,7 +396,7 @@
381396
});
382397

383398
// Make the request with the body.
384-
xhr.send(options.body);
399+
xhr.send(body);
385400
};
386401
} else {
387402
var url = require('url');
@@ -531,7 +546,7 @@
531546
'Content-Type': 'application/x-www-form-urlencoded',
532547
'Authorization': 'Basic ' + authorization
533548
},
534-
body: uriEncode({
549+
data: uriEncode({
535550
refresh_token: this.refreshToken,
536551
grant_type: 'refresh_token'
537552
})
@@ -593,7 +608,7 @@
593608
'Content-Type': 'application/x-www-form-urlencoded',
594609
'Authorization': 'Basic ' + authorization
595610
},
596-
body: uriEncode({
611+
data: uriEncode({
597612
scope: sanitizeScope(options.scopes),
598613
username: username,
599614
password: password,
@@ -743,7 +758,7 @@
743758
'Content-Type': 'application/x-www-form-urlencoded',
744759
'Authorization': 'Basic ' + authorization
745760
},
746-
body: uriEncode({
761+
data: uriEncode({
747762
scope: sanitizeScope(options.scopes),
748763
grant_type: 'client_credentials'
749764
})
@@ -855,7 +870,7 @@
855870
'Accept': 'application/json, application/x-www-form-urlencoded',
856871
'Content-Type': 'application/x-www-form-urlencoded'
857872
},
858-
body: uriEncode({
873+
data: uriEncode({
859874
code: query.code,
860875
grant_type: 'authorization_code',
861876
redirect_uri: options.redirectUri,

0 commit comments

Comments
 (0)