Stricter JSON parse when content-type header is application/json. #2

Merged
merged 1 commit into from Dec 5, 2012
View
@@ -28,9 +28,13 @@ Chargify.prototype.request = function(options, callback) {
};
request(options, function(err, res, body) {
if (err) return callback(err);
- try {
- var body = JSON.parse(body);
- } catch(e) {}
+ if (res.headers['content-type'].indexOf('application/json') !== -1 && typeof body !== 'object') {
+ try {
+ res.body = body = JSON.parse(body);
+ } catch(e) {
+ return callback(e);
+ }
+ }
callback(err, res, body);
});
};