Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch fails for responses with http status 421 when the request has a body #2898

Closed
bdemann opened this issue Mar 1, 2024 · 0 comments · Fixed by #2900
Closed

Fetch fails for responses with http status 421 when the request has a body #2898

bdemann opened this issue Mar 1, 2024 · 0 comments · Fixed by #2900
Labels
bug Something isn't working fetch

Comments

@bdemann
Copy link

bdemann commented Mar 1, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fetch
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants