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

shabunc picture shabunc · Dec 22, 2016 · Viewed 20.5k times · Source

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.

Answer

robertklep picture robertklep · Dec 22, 2016

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();
})