[Fix] net::ERR_CONTENT_LENGTH_MISMATCH 200 (OK)

Recently when I was coding with the HTTP1.1 server in kotlin,I got an unexpected error when I tried Running it and opening the browser at localhost:5555; net::ERR_CONTENT_LENGTH_MISMATCH 200 (OK). A snippet of the error I got is given below:

I would like to share the steps that helped me to fix the error; net::ERR_CONTENT_LENGTH_MISMATCH 200 (OK):

Why net::ERR_CONTENT_LENGTH_MISMATCH 200 (OK) error is seen?

The error, net::ERR_CONTENT_LENGTH_MISMATCH 200 (OK) is seen because when the code is synchronous, all its logic should be implemented inside CompletionHandler to handle proper debugging.

The error is also caused by the socket channels as the implementation ultimately depends on the version of the HTTP protocol. You also have to write your code into the socket properly. If this step is not maintained it will throw an error.

It is mandatory as the java.nio does not route all errors to CompletionHandler#failed. One more reason why it is important to know the proper socket format is that it does not send a full buffer to the client.

A simple solution would be to implement all your code, and its logic inside CompletionHandler to handle proper debugging. The error also arises because the code may be written according to the socket.

A detailed solution to fix the net::ERR_CONTENT_LENGTH_MISMATCH 200 (OK) error is provided below:

How to fix net::ERR_CONTENT_LENGTH_MISMATCH 200 (OK) error?

To resolve the error net::ERR_CONTENT_LENGTH_MISMATCH 200 (OK), You need to implement all your code, and it’s logic inside a CompletionHandler to handle proper debugging.

There is also one more thing about your code you need to take care is if you are still facing the error:

If you are writing to socket then your code snippet should look like the below-mentioned code:

public <A> void completed(Integer result, A attachment) {
    if (result < 0) {
        // socket closed
        channel.close();
        return;
    }
    if (buffer.hasRemaining()) {
        channel.write(buffer, null, this);
    }
} 

This way of writing code is mandatory because of the following reasons:

Reason 1: The first reason is because java.nio does not route all errors to CompletionHandler#failed.

Reason 2: The second reason is that it does not send a full buffer to the client.

Following these steps should fix the error

Conclusion

To fix the net::ERR_CONTENT_LENGTH_MISMATCH 200 (OK) error, you would have to implement all your code and it’s logic inside a CompletionHandler to handle proper debugging. The error also arises because the code may not be written according to the socket.