"Weird" utf-8 characters in POST body causing content-length mismatch
Problem
When I submit a POST request and the body contains characters like "å", the server responds with a 400 because the content-length didn't match. I'm guessing that the problem is that multiple byte utf-8 characters are being assumed to be only one byte long when they are counted. I'm not sure if this is a bug in express or connect or raw-body. Hopefully you can pass it along if it's not an express bug. This bug only seems to have appeared in express versions 3.4.2 and later. Here's the error message: [code block]
Error Output
Error: request size did not match content length] status: 400, received: 545, expected: 546 } 'Error: request size did not match content length\n at IncomingMessage.onEnd (/home/user/my-project/node_modul
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Fix UTF-8 Content-Length Mismatch in Express POST Requests
The issue arises because the Content-Length header is calculated based on the byte count of the request body, but when the body contains multi-byte UTF-8 characters (like 'å'), the byte count may not match the expected length if the encoding is not handled correctly. This discrepancy leads to a 400 error response from the server.
Awaiting Verification
Be the first to verify this fix
- 1
Update Express Middleware
Ensure that the body-parser middleware is configured to handle UTF-8 characters correctly. Use the 'utf-8' option in the body-parser to ensure proper encoding.
javascriptconst bodyParser = require('body-parser'); app.use(bodyParser.json({ limit: '1mb', type: 'application/json', charset: 'utf-8' })); - 2
Set Correct Content-Type Header
Make sure that the client sending the POST request sets the Content-Type header to 'application/json; charset=utf-8'. This informs the server about the character encoding used in the request body.
javascriptfetch('/api/endpoint', { method: 'POST', headers: { 'Content-Type': 'application/json; charset=utf-8' }, body: JSON.stringify({ key: 'value with å' }) }); - 3
Verify Server Response Handling
Check the server's response handling to ensure that it correctly interprets the incoming request body. If using raw-body, ensure it is set up to handle UTF-8 encoding appropriately.
javascriptconst getRawBody = require('raw-body'); app.post('/api/endpoint', (req, res) => { getRawBody(req, { encoding: 'utf-8' }, (err, string) => { if (err) return res.status(400).send(err); // Process string }); }); - 4
Test with Various UTF-8 Characters
After making the changes, test the POST request with various UTF-8 characters to ensure that the content-length matches the actual byte length of the request body.
javascriptconst testPayload = { key: 'value with å, ö, and ü' }; // Test payload with multi-byte characters // Send POST request with testPayload
Validation
Confirm that the server no longer returns a 400 error for POST requests containing multi-byte UTF-8 characters. Check the logs to ensure that the received content-length matches the expected content-length.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep