Skip to content

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

Description

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions