From 91006f721b87a0ac49059c2f9dd969815f407c55 Mon Sep 17 00:00:00 2001 From: Justin York Date: Tue, 4 Dec 2012 11:25:09 -0700 Subject: [PATCH 1/3] Mix default headers and query options with specific settings from the request --- lib/restler.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/restler.js b/lib/restler.js index bf6cc57..c7eb50a 100644 --- a/lib/restler.js +++ b/lib/restler.js @@ -470,6 +470,16 @@ mixin(Service.prototype, { }, _withDefaults: function(options) { var o = mixin({}, this.defaults); + if( options ) { + if(o.headers && options.headers) { + mixin(o.headers, options.headers); + delete options.headers; + } + if(o.query && options.query) { + mixin(o.query, options.query); + delete options.query; + } + } return mixin(o, options); } }); From 946f58c1322672f5092e83ea9837239416058f1c Mon Sep 17 00:00:00 2001 From: Justin York Date: Tue, 4 Dec 2012 12:37:07 -0700 Subject: [PATCH 2/3] calling retry with post data string sends garbled data --- lib/restler.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/restler.js b/lib/restler.js index c7eb50a..dec5620 100644 --- a/lib/restler.js +++ b/lib/restler.js @@ -63,12 +63,15 @@ 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') { + if (this.options.data instanceof Buffer) { + this.options.data = new Buffer(this.options.data.toString(), this.options.encoding || 'utf8'); + } + 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') { + else 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; @@ -229,7 +232,6 @@ mixin(Request.prototype, { }, run: function() { var self = this; - if (this.options.multipart) { multipart.write(this.request, this.options.data, function() { self.request.end(); From 4c3814a616e06009a6e491fc875babcecef726fb Mon Sep 17 00:00:00 2001 From: Justin York Date: Sat, 8 Dec 2012 02:29:40 -0700 Subject: [PATCH 3/3] Don't catch all exceptions from request event handlers when trying to parse JSON --- lib/restler.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/restler.js b/lib/restler.js index dec5620..6807268 100644 --- a/lib/restler.js +++ b/lib/restler.js @@ -370,12 +370,14 @@ var parsers = { }, json: function(data, callback) { if (data && data.length) { + var json = null, + err = null; try { - callback(null, JSON.parse(data)); - } catch (err) { + json = JSON.parse(data); + } catch (err) { err.message = 'Failed to parse JSON body: ' + err.message; - callback(err, null); } + callback(err, json); } else { callback(null, null); }