Browse Source

added support for PUT content body and handle JSON error responses

pull/2/merge
Brett Langdon 13 years ago
parent
commit
1aff5f18af
1 changed files with 8 additions and 5 deletions
  1. +8
    -5
      lib/http_client.js

+ 8
- 5
lib/http_client.js View File

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


Loading…
Cancel
Save