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()