From 1aff5f18af959b11dab59be78bc80a9010936ded Mon Sep 17 00:00:00 2001 From: Brett Langdon Date: Wed, 27 Mar 2013 17:03:15 -0400 Subject: [PATCH] added support for PUT content body and handle JSON error responses --- lib/http_client.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/http_client.js b/lib/http_client.js index 9212f9e..699ad87 100644 --- a/lib/http_client.js +++ b/lib/http_client.js @@ -96,7 +96,7 @@ client.prototype.request = function(method, path, params, callback){ path: path, }; - if(http_options['method'] == 'POST'){ + if(['POST', 'PUT'].indexOf(http_options['method']) >= 0){ http_options['headers'] = { 'Content-Type': 'application/json', 'Content-Length': body.length, @@ -116,17 +116,20 @@ client.prototype.request = function(method, path, params, callback){ }); res.on('end', function(){ - if(res.headers['content-type'] == 'application/json'){ - data = JSON.parse(data); + error = null; + try{ data = JSON.parse(data); }catch(e){} + if(data['errors']){ + error = data['errors']; + data = null; } if(v8type.is(callback, v8type.FUNCTION)){ - callback(null, data, res.statusCode); + callback(error, data, res.statusCode); } }); }); - if(http_options['method'] == 'POST'){ + if(['POST', 'PUT'].indexOf(http_options['method']) >= 0){ req.write(body); } req.end()