busboy icon indicating copy to clipboard operation
busboy copied to clipboard

Missing Content-Type

Open yogeshzairfoil opened this issue 2 years ago • 7 comments

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"));
  });
};

yogeshzairfoil avatar May 18 '23 00:05 yogeshzairfoil

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.

mscdex avatar May 18 '23 01:05 mscdex

@mscdex I logged it. It is lower case image

yogeshzairfoil avatar May 18 '23 04:05 yogeshzairfoil

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.

yogeshzairfoil avatar May 18 '23 04:05 yogeshzairfoil

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?

mscdex avatar May 18 '23 10:05 mscdex

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

yogeshzairfoil avatar May 18 '23 18:05 yogeshzairfoil

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.

mscdex avatar May 18 '23 20:05 mscdex

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) });

textrix avatar Dec 27 '23 08:12 textrix