ERROR 5400: Bad request: Error parsing form fields with Node

Hi,

I have the following POST handler in my node (Remix) server:

export async function action({ request }: ActionArgs) {
  invariant(process.env.CF_ACCT_ID);
  invariant(process.env.CF_IMG_API_TOKEN);

  const data = await request.formData();
  const file = data.get("file") as File;

  console.log(file.name, file.size);

  let formData = new FormData();
  formData.append("size", String(file.size)); // size must be first!
  formData.append("file", file, file.name);

  const cfUrl = `https://api.cloudflare.com/client/v4/accounts/${process.env.CF_ACCT_ID}/images/v1`;
  const response = await fetch(cfUrl, {
    method: "POST",
    headers: {
      // "Content-Type": "multipart/form-data",
      Authorization: `Bearer ${process.env.CF_IMG_API_TOKEN}`,
    },
    body: formData,
  });

  console.log(await response.text());

  return json("ok");
}

Here is the error output:

devon.jpg 257378
ERROR 5400: Bad request: Error parsing form fields

If I uncomment "Content-Type": "multipart/form-data", I have this error output:

devon.jpg 257378
ERROR 5400: Bad request: multipart/form-data Content-Type has invalid syntax: lacks boundary

Any help would be greatly appreciated :pray:

Ok my bad I tried everything I found on CF community and on the web. Removing the formData “size” solved it.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.