diff --git a/request.js b/request.js index 97e07e667..6bdd51436 100644 --- a/request.js +++ b/request.js @@ -1321,7 +1321,7 @@ Request.prototype.onRequestResponse = function (response) { } debug('emitting complete', self.uri.href) if(typeof response.body === 'undefined' && !self._json) { - response.body = '' + response.body = self.encoding === null ? new Buffer(0) : '' } self.emit('complete', response, response.body) }) diff --git a/tests/test-emptyBody.js b/tests/test-emptyBody.js index 68b2f6cdb..86bc6c0e9 100644 --- a/tests/test-emptyBody.js +++ b/tests/test-emptyBody.js @@ -15,7 +15,7 @@ tape('setup', function(t) { }) }) -tape('empty body', function(t) { +tape('empty body with encoding', function(t) { request('http://localhost:6767', function(err, res, body) { t.equal(err, null) t.equal(res.statusCode, 200) @@ -24,6 +24,18 @@ tape('empty body', function(t) { }) }) +tape('empty body without encoding', function(t) { + request({ + url: 'http://localhost:6767', + encoding: null + }, function(err, res, body) { + t.equal(err, null) + t.equal(res.statusCode, 200) + t.same(body, new Buffer(0)) + t.end() + }) +}) + tape('empty JSON body', function(t) { request({ url: 'http://localhost:6767',