mscdex / busboy

A streaming parser for HTML form data for node.js
MIT License
2.84k stars 213 forks source link

Missing Content-Type #341

Open yogeshzairfoil opened 1 year ago

yogeshzairfoil commented 1 year ago

I was under the impression that this issues had been resolved but it still persists. I have tried both Camel case and Lower case content-type but none of it works. Here is my code

import busboy from "busboy";

type Fields = {
  image: {
    filename: string;
    type: string;
    content: Buffer;
  }[];
};

export const parseMultipartForm = (event): Promise<Fields> => {
  return new Promise((resolve) => {
    const fields = { image: [] };
    const bb = busboy({
      headers: {
        ...event.headers,
        "content-type":
          event.headers["Content-Type"] || event.headers["content-type"],
      },
    });
    bb.on("field", (fieldname, value, info) => {
      console.log(fieldname);s
    });
    bb.on("file", (name, file, info) => {
      const { filename, mimeType } = info;

      file.on("data", (data) => {
        if (!fields[name]) fields[name] = [];

        fields[name].push({
          filename,
          type: mimeType,
          content: data,
        });
      });
    });

    bb.on("close", () => {
      resolve(fields);
    });

    bb.end(Buffer.from(event.body, "base64"));
  });
};
mscdex commented 1 year ago

Have you checked the contents of event.headers to know what is the proper casing? It must be something other than the two you're attempting to access.

yogeshzairfoil commented 1 year ago

@mscdex I logged it. It is lower case

image
yogeshzairfoil commented 1 year ago

headers: {
        ...event.headers,
        "Content-Type":
          event.headers["Content-Type"] || event.headers["content-type"],
      }```

      I tried updating the string here to have to exactly same case the error is mentioning.
mscdex commented 1 year ago

I don't know what to tell you then as event.headers seems to already have only lowercased header names. The only way you can get the "Missing Content-Type" error is if the headers object you pass to busboy is a non-object, null, or if headers['content-type'] is not a string.

Just to double check, what does typeof event.headers['content-type'] show?

yogeshzairfoil commented 1 year ago

It logs back as string

I really feel there might be something on Busboy's end that is messing this up. I think people still have these problems with lamda functions - saw that when I was going through all the issues here

@mscdex

mscdex commented 1 year ago

Well, you'll need to dig into the code and troubleshoot it yourself then because I have no explanation as to why you're receiving that particular error in this case.

textrix commented 8 months ago

It's not a case-sensitivity issue with content-type. The problem seems to be that the cfg.headers['content-type'] statement returns undefined because the headers in the routing endpoint's request are not a plain map. This is because busboy is not getting the value in the form of headers.get('content-type'), but using index brackets. To make it more convenient, it seems like a good idea to ask busboy authors to use headers.get.

In a nutshell, try this:

    const simpleHeaders = {};
    for (const key of request.headers.keys()) {
        simpleHeaders[key] = request.headers.get(key);
    }

    const busboy = Busboy({ headers: simpleHeaders });

or

    const busboy = Busboy({ headers: Object.fromEntries(request.headers) });