From 4362ccafbb3b4fba28815afb370f94a167231658 Mon Sep 17 00:00:00 2001 From: Jeff Stamerjohn Date: Thu, 11 Jul 2013 10:10:40 -0700 Subject: [PATCH 1/4] Fix request retries --- lib/restler.js | 28 +++++++++++++++------------- package.json | 2 +- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/lib/restler.js b/lib/restler.js index 2664233..78e77ff 100644 --- a/lib/restler.js +++ b/lib/restler.js @@ -63,16 +63,17 @@ function Request(uri, options) { console.log("Building multipart request without Content-Length header, please specify all file sizes"); } } else { - if (typeof this.options.data == 'object') { - this.options.data = qs.stringify(this.options.data); - this.headers['Content-Type'] = 'application/x-www-form-urlencoded'; - this.headers['Content-Length'] = this.options.data.length; - } - if(typeof this.options.data == 'string') { - var buffer = new Buffer(this.options.data, this.options.encoding || 'utf8'); - this.options.data = buffer; - this.headers['Content-Length'] = buffer.length; - } + if (!this.data) { + if (typeof this.options.data == 'object') { + this.data = qs.stringify(this.options.data); + this.headers['Content-Type'] = 'application/x-www-form-urlencoded'; + this.headers['Content-Length'] = this.options.data.length; + } + if(typeof this.options.data == 'string') { + var buffer = new Buffer(this.options.data, this.options.encoding || 'utf8'); + this.data = buffer; + this.headers['Content-Length'] = buffer.length; + } } var proto = (this.url.protocol == 'https:') ? https : http; @@ -125,6 +126,7 @@ mixin(Request.prototype, { self.url = url.parse(url.resolve(self.url.href, response.headers['location'])); self.options.method = 'GET'; delete self.options.data; + delete self.data; self._retry(); } else { self.url = url.parse(url.resolve(self.url.href, response.headers['location'])); @@ -239,12 +241,12 @@ mixin(Request.prototype, { run: function() { var self = this; if (this.options.multipart) { - multipart.write(this.request, this.options.data, function() { + multipart.write(this.request, this.data, function() { self.request.end(); }); } else { - if (this.options.data) { - this.request.write(this.options.data.toString(), this.options.encoding || 'utf8'); + if (this.data) { + this.request.write(this.data.toString(), this.options.encoding || 'utf8'); } this.request.end(); } diff --git a/package.json b/package.json index c96f316..3fe5d78 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "restler", - "version": "2.0.1", + "version": "2.0.2", "description": "An HTTP client library for node.js", "contributors": [{ "name": "Dan Webb", "email": "dan@danwebb.net" }], "homepage": "https://github.com/danwrong/restler", From 59caaaf759c0f102a7e96f8428275aa276044c2b Mon Sep 17 00:00:00 2001 From: Jeff Stamerjohn Date: Thu, 11 Jul 2013 10:39:56 -0700 Subject: [PATCH 2/4] Add missing } --- lib/restler.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/restler.js b/lib/restler.js index 78e77ff..bb85ddd 100644 --- a/lib/restler.js +++ b/lib/restler.js @@ -74,6 +74,7 @@ function Request(uri, options) { this.data = buffer; this.headers['Content-Length'] = buffer.length; } + } } var proto = (this.url.protocol == 'https:') ? https : http; From 38c301f77277a6a583141190934b30f37c135e35 Mon Sep 17 00:00:00 2001 From: Jeff Stamerjohn Date: Thu, 11 Jul 2013 11:00:30 -0700 Subject: [PATCH 3/4] Fix content length --- lib/restler.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/restler.js b/lib/restler.js index bb85ddd..aba2c09 100644 --- a/lib/restler.js +++ b/lib/restler.js @@ -67,7 +67,7 @@ function Request(uri, options) { if (typeof this.options.data == 'object') { this.data = qs.stringify(this.options.data); this.headers['Content-Type'] = 'application/x-www-form-urlencoded'; - this.headers['Content-Length'] = this.options.data.length; + this.headers['Content-Length'] = this.data.length; } if(typeof this.options.data == 'string') { var buffer = new Buffer(this.options.data, this.options.encoding || 'utf8'); From f84314df710aab14648622af1f5e11e6caeec985 Mon Sep 17 00:00:00 2001 From: Jeff Stamerjohn Date: Thu, 11 Jul 2013 11:27:50 -0700 Subject: [PATCH 4/4] Convert data strings to buffers (ensuring correct content-length) --- lib/restler.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/restler.js b/lib/restler.js index aba2c09..83a68a4 100644 --- a/lib/restler.js +++ b/lib/restler.js @@ -64,13 +64,14 @@ function Request(uri, options) { } } else { if (!this.data) { - if (typeof this.options.data == 'object') { - this.data = qs.stringify(this.options.data); + this.data = this.options.data; + if (typeof this.data == 'object') { + this.data = qs.stringify(this.data); this.headers['Content-Type'] = 'application/x-www-form-urlencoded'; this.headers['Content-Length'] = this.data.length; } - if(typeof this.options.data == 'string') { - var buffer = new Buffer(this.options.data, this.options.encoding || 'utf8'); + if(typeof this.data == 'string') { + var buffer = new Buffer(this.data, this.options.encoding || 'utf8'); this.data = buffer; this.headers['Content-Length'] = buffer.length; }