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

Missing Content-Type #341

Open
yogeshzairfoil opened this issue May 18, 2023 · 7 comments
Open

Missing Content-Type #341

yogeshzairfoil opened this issue May 18, 2023 · 7 comments
Labels

Comments

@yogeshzairfoil
Copy link

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
Copy link
Owner

mscdex commented May 18, 2023

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
Copy link
Author

@mscdex I logged it. It is lower case
image

@yogeshzairfoil
Copy link
Author

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
Copy link
Owner

mscdex commented May 18, 2023

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
Copy link
Author

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
Copy link
Owner

mscdex commented May 18, 2023

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
Copy link

textrix commented Dec 27, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants