Skip to content

Commit 90d743e

Browse files
author
Michal Filipek
committed
fix form-url-encoding data (body) for nested objects
1 parent f506898 commit 90d743e

File tree

2 files changed

+55
-10
lines changed

2 files changed

+55
-10
lines changed

iron-request.html

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -413,16 +413,43 @@
413413
return '';
414414
}
415415
var pieces = [];
416-
Object.keys(object).forEach(function(key) {
417-
// TODO(rictic): handle array values here, in a consistent way with
418-
// iron-ajax params.
419-
pieces.push(
420-
this._wwwFormUrlEncodePiece(key) + '=' +
421-
this._wwwFormUrlEncodePiece(object[key]));
422-
}, this);
416+
pieces = this._wwwFormUrlEncodeRecurrent(null, object , pieces);
423417
return pieces.join('&');
424418
},
425-
419+
/**
420+
* Funcion parse recurrently whole object. Support multi level nodes
421+
*
422+
* @param {String} keyprefix The prefix of key (parent name).
423+
* @param {Object} object The object (or part of it) to encode
424+
* as x-www-form-urlencoded.
425+
* @param {Array} container Array which contains all parsed elements.
426+
* @return {Array} array with parsed element
427+
*/
428+
_wwwFormUrlEncodeRecurrent: function(keyprefix, object, container) {
429+
if(!container){
430+
container = [];
431+
}
432+
if(!object){
433+
return container;
434+
}
435+
Object.keys(object).forEach(function(key) {
436+
var keyWithPrefix;
437+
if(!keyprefix){
438+
keyWithPrefix = key;
439+
}else{
440+
keyWithPrefix = keyprefix + '[' + key + ']';
441+
}
442+
if((Object.keys(object[key]).length > 0) && (typeof object[key] != 'string')){
443+
container = this._wwwFormUrlEncodeRecurrent(keyWithPrefix, object[key], container);
444+
} else {
445+
container.push(
446+
this._wwwFormUrlEncodePiece(keyWithPrefix) + '=' +
447+
this._wwwFormUrlEncodePiece(object[key]));
448+
}
449+
}
450+
, this);
451+
return container;
452+
},
426453
/**
427454
* @param {*} str A key or value to encode as x-www-form-urlencoded.
428455
* @return {string} .
@@ -433,7 +460,6 @@
433460
return encodeURIComponent(str.toString().replace(/\r?\n/g, '\r\n'))
434461
.replace(/%20/g, '+');
435462
},
436-
437463
/**
438464
* Updates the status code and status text.
439465
*/
@@ -443,4 +469,3 @@
443469
}
444470
});
445471
</script>
446-

test/iron-ajax.html

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,26 @@
499499
'foo=bar%0D%0Abip&biz+bo=baz+blar');
500500
});
501501

502+
test('if `contentType` is set to form encode, the body is encoded. \
503+
Body Object contains nested objects',function() {
504+
ajax.body = {
505+
arraybar: [1,2,3],
506+
"foo":"bar",
507+
"fo": {
508+
br : 1,
509+
ba : "1",
510+
bb : [],
511+
nc : [1,2,3]
512+
}
513+
};
514+
ajax.contentType = 'application/x-www-form-urlencoded';
515+
ajax.generateRequest();
516+
517+
expect(server.requests[0]).to.be.ok;
518+
expect(server.requests[0].requestBody).to.be.equal(
519+
'arraybar%5B0%5D=1&arraybar%5B1%5D=2&arraybar%5B2%5D=3&foo=bar&fo%5Bbr%5D=1&fo%5Bba%5D=1&fo%5Bbb%5D=&fo%5Bnc%5D%5B0%5D=1&fo%5Bnc%5D%5B1%5D=2&fo%5Bnc%5D%5B2%5D=3');
520+
});
521+
502522
test('if `contentType` is json, the body is json encoded', function() {
503523
var requestObj = {foo: 'bar', baz: [1,2,3]}
504524
ajax.body = requestObj;

0 commit comments

Comments
 (0)