diff --git a/iron-request.html b/iron-request.html
index 57e66fe..2803e48 100644
--- a/iron-request.html
+++ b/iron-request.html
@@ -413,16 +413,43 @@
return '';
}
var pieces = [];
- Object.keys(object).forEach(function(key) {
- // TODO(rictic): handle array values here, in a consistent way with
- // iron-ajax params.
- pieces.push(
- this._wwwFormUrlEncodePiece(key) + '=' +
- this._wwwFormUrlEncodePiece(object[key]));
- }, this);
+ pieces = this._wwwFormUrlEncodeRecurrent(null, object , pieces);
return pieces.join('&');
},
-
+ /**
+ * Funcion parse recurrently whole object. Support multi level nodes
+ *
+ * @param {String} keyprefix The prefix of key (parent name).
+ * @param {Object} object The object (or part of it) to encode
+ * as x-www-form-urlencoded.
+ * @param {Array} container Array which contains all parsed elements.
+ * @return {Array} array with parsed element
+ */
+ _wwwFormUrlEncodeRecurrent: function(keyprefix, object, container) {
+ if(!container){
+ container = [];
+ }
+ if(!object){
+ return container;
+ }
+ Object.keys(object).forEach(function(key) {
+ var keyWithPrefix;
+ if(!keyprefix){
+ keyWithPrefix = key;
+ }else{
+ keyWithPrefix = keyprefix + '[' + key + ']';
+ }
+ if((Object.keys(object[key]).length > 0) && (typeof object[key] != 'string')){
+ container = this._wwwFormUrlEncodeRecurrent(keyWithPrefix, object[key], container);
+ } else {
+ container.push(
+ this._wwwFormUrlEncodePiece(keyWithPrefix) + '=' +
+ this._wwwFormUrlEncodePiece(object[key]));
+ }
+ }
+ , this);
+ return container;
+ },
/**
* @param {*} str A key or value to encode as x-www-form-urlencoded.
* @return {string} .
@@ -433,7 +460,6 @@
return encodeURIComponent(str.toString().replace(/\r?\n/g, '\r\n'))
.replace(/%20/g, '+');
},
-
/**
* Updates the status code and status text.
*/
@@ -443,4 +469,3 @@
}
});
-
diff --git a/test/iron-ajax.html b/test/iron-ajax.html
index b05f875..3600c67 100644
--- a/test/iron-ajax.html
+++ b/test/iron-ajax.html
@@ -499,6 +499,26 @@
'foo=bar%0D%0Abip&biz+bo=baz+blar');
});
+ test('if `contentType` is set to form encode, the body is encoded. \
+ Body Object contains nested objects',function() {
+ ajax.body = {
+ arraybar: [1,2,3],
+ "foo":"bar",
+ "fo": {
+ br : 1,
+ ba : "1",
+ bb : [],
+ nc : [1,2,3]
+ }
+ };
+ ajax.contentType = 'application/x-www-form-urlencoded';
+ ajax.generateRequest();
+
+ expect(server.requests[0]).to.be.ok;
+ expect(server.requests[0].requestBody).to.be.equal(
+ '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');
+ });
+
test('if `contentType` is json, the body is json encoded', function() {
var requestObj = {foo: 'bar', baz: [1,2,3]}
ajax.body = requestObj;