diff --git a/src/node/index.js b/src/node/index.js index bb7b2f7bb..c7e77c5de 100644 --- a/src/node/index.js +++ b/src/node/index.js @@ -952,6 +952,7 @@ Request.prototype._end = function() { let data = this._data; const { req } = this; const { method } = this; + const methodsWithBody = ['PUT', 'POST', 'PATCH']; this._setTimeouts(); @@ -971,7 +972,11 @@ Request.prototype._end = function() { } // content-length - if (data && !req.getHeader('Content-Length')) { + if ( + methodsWithBody.includes(method) && + data && + !req.getHeader('Content-Length') + ) { req.setHeader( 'Content-Length', Buffer.isBuffer(data) ? data.length : Buffer.byteLength(data) diff --git a/test/request.js b/test/request.js index 9e5bc2f56..8c36a4167 100644 --- a/test/request.js +++ b/test/request.js @@ -172,6 +172,21 @@ describe('request', function() { }); }); + it('GET should not send the content-length header', next => { + request + .get(`${uri}/content-length`) + .send({ foo: 'bar' }) + .then(res => { + try { + assert(!res.badRequest); + next(); + } catch (err) { + next(err); + } + }) + .catch(next); + }); + it('get()', next => { request.get(`${uri}/notfound`).end((err, res) => { try { diff --git a/test/support/server.js b/test/support/server.js index b06b0c0a2..0cc9af08a 100644 --- a/test/support/server.js +++ b/test/support/server.js @@ -578,5 +578,17 @@ app.get('/error/redirect-error:id', (req, res) => { } }); +app.get('/content-length', (req, res) => { + const { headers } = req; + if ( + headers.hasOwnProperty('content-length') && + headers['content-length'] > 0 + ) { + res.status(400).send('bad request'); + } else { + res.status(200).send('ok'); + } +}); + const server = http.createServer(app); server.listen(process.env.ZUUL_PORT);