-
-
Notifications
You must be signed in to change notification settings - Fork 671
Description
Bug Description
If the client sends a request with a body of length greater than 0 and the server responds with a 421 status then fetch will fail.
Reproducible By
With an express server (for example) with an endpoint like this
app.post('/421', (req, res) => {
res.status(421).send('This is a 421 Misdirected Request response.');
});Being hit with fetch like this
async function fetch421Endpoint() {
try {
const response = await fetch('http://localhost:3000/421', {
method: 'POST',
body: new Uint8Array(Buffer.from('helloworld', 'utf-8'))
});
if (response.status === 421) {
const data = await response.text();
console.log('421 endpoint response:', data);
} else {
console.log('421 endpoint didn\'t return with 421 status');
console.error('Error:', response.status, response.statusText);
}
} catch (error) {
console.log('421 endpoint failed');
console.error('Error:', error);
}
}Expected Behavior
In the above example I would expect it to print out "421 endpoint response: This is a 421 Misdirected Request response." But instead it throws and it lands in the catch block. cURL, FireFox's and Chrome's implementation of fetch both have the expected behavior.
Because it only happens with request bodies of length 1 or greater, GET requests always work as expected
Logs & Screenshots
Error: TypeError: fetch failed
at node:internal/deps/undici/undici:12344:11
at async fetch421Endpoint (fourZeroSeven/fetch.js:38:22) {
cause: RequestContentLengthMismatchError: Request body length does not match content-length header
at AsyncWriter.end (node:internal/deps/undici/undici:9742:19)
at writeIterable (node:internal/deps/undici/undici:9646:16)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
code: 'UND_ERR_REQ_CONTENT_LENGTH_MISMATCH'
}
}
Environment
I have tried on the following node version: v18.18.0 and v21.6.1
Ubuntu 23.10
Additional context
Additional information and a full server and client reproduction can be found here https://github.com/demergent-labs/fourZeroSeven