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

Undici didn't update Content-Type header for multipart/form-data requests after following redirects which leads to mismatch in request body and header #4065

Open
AndriiHe opened this issue Feb 21, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@AndriiHe
Copy link

Bug Description

There is a problem when sending a multipart/form-data request that follows redirects. After the redirect, undici overrides the request body but does not update the Content-Type header, which causes an error when parsing the body. The updated content type just ignored here.

Reproducible By

const { createServer } = require("http");
const { fetch, FormData } = require("undici");

createServer((req, res) => {
  if (req.method === "POST" && req.url === "/first") {
    res.writeHead(307, {
      Location: "http://localhost:3000/second",
    });
    res.end();
  } else if (req.method === "POST" && req.url === "/second") {
    console.log("Request headers:\n", req.headers);

    let body = "";
    req.on("data", (chunk) => {
      body += chunk;
    });

    req.on("end", () => {
      console.log("Request body:\n", body);
      res.writeHead(200);
      res.end("Request received");
    });
  }
}).listen(3000);

// Make a request to the first endpoint using undici
async function makeRequest() {
  const formData = new FormData();
  formData.append("test", "data");

  const response = await fetch("http://localhost:3000/first", {
    method: "POST",
    body: formData,
    redirect: "follow",
  });

  console.log("Response status:\n", response.status);
}

makeRequest().catch(console.error);

Expected Behavior

The Content-Type header should be updated along with the body after a redirect.

Logs & Screenshots

Request headers:
 {
  host: 'localhost:3000',
  connection: 'keep-alive',
  'content-type': 'multipart/form-data; boundary=----formdata-undici-073413599880',
  accept: '*/*',
  'accept-language': '*',
  'sec-fetch-mode': 'cors',
  'user-agent': 'undici',
  'accept-encoding': 'gzip, deflate',
  'content-length': '127'
}
Request body:
 ------formdata-undici-045095077684
Content-Disposition: form-data; name="test"

data
------formdata-undici-045095077684--

Environment

Additional context

@AndriiHe AndriiHe added the bug Something isn't working label Feb 21, 2025
@AndriiHe AndriiHe changed the title Undici fails to update Content-Type header after following redirects multipart/form-data requests Undici didn't update Content-Type header after following redirects multipart/form-data requests which leads to mismatch in request body and header Feb 21, 2025
@AndriiHe AndriiHe changed the title Undici didn't update Content-Type header after following redirects multipart/form-data requests which leads to mismatch in request body and header Undici didn't update Content-Type header for multipart/form-data requests after following redirects which leads to mismatch in request body and header Feb 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant