How to get net::ERR CONTENT LENGTH MISMATCH in Chrome using express

33.8k views Asked by At

For testing purposes I want to see in browser infamous error net::ERR CONTENT LENGTH MISMATCH. But the thing is I don't know how. Naïve attempt to just pass wrong Content-Length seems not to be working - all the clients just truncate content (btw - is it some well-established, RFC documented behavior?).

Here's a code you can try:

var express = require('express');
var app = express();

app.get('/ping', function (req, res) {
  res.set({
    'Content-Length': 7,  
  }); 

  // don't use res.send, 
  //it will ignore explicitly set Content-Length in favor of the right one
  res.write('ABCDEFGHIJKLMNOP');
  res.end();
})

app.listen(3000, function () {
  console.log('http://127.0.0.1:3000');
});

The same holds true for minimal python server. Also I had a crazy thought that may be express truncates response whenever content-length is set, but tcpdump clearly shows that on client side whole body is received.

2

There are 2 answers

0
robertklep On BEST ANSWER

It looks like most clients don't care if the server sends more data than is stated in Content-Length. However, if you send less data, and make sure that the connection gets closed server-side (the client won't close it because it thinks there's still more data to come), you can trigger net::ERR_CONTENT_LENGTH_MISMATCH:

app.get('/ping', function (req, res) {
  res.set({ 'Content-Length': 70 });
  res.write('ABCDEFGHIJKLMNOP');
  res.end();
  res.connection.end();
})
0
White Marcus On

I fixed setting the server.keepAliveTimeout

var httpsServer = https.createServer({
    key: fs.readFileSync('key.pem'),
    cert: fs.readFileSync('cert.pem'),
    ca: fs.readFileSync('intermediate.crt')
}, app).listen(port);
httpsServer.keepAliveTimeout = 60000 * 2;
httpsServer.listen(port);