[SOLVED] Web3Api.storage.uploadFolder: PayloadTooLargeError

I run the moralis server locally.

use this kind of code:

  const uploadFolder = async (fileName: string, file: File) => {
    const base64 = await convertBase64(file);
    const content = String(base64).replace(/^data:image\/[a-z]+;base64,/, "")
    const options = {
      abi: [
        {
          path: `images/${fileName}`,
          content
        },
      ],
    };
    const path = await Web3Api.storage.uploadFolder(options);
    return path
  };

where content is the base64 encoding of an existing file - about 100KB.
The server response has the status code 413 Payload Too Large
PayloadTooLargeError: request entity too large

Any suggestions on how i can avoid this error? Thank you

It works with smaller files?
It should work without issues for 100kb

71KB or less is ok, 81KB or above is not ok.
is there a size limit in place somewhere?

p.s. the convertBase64 method:

const convertBase64 = (file: File) => {
    return new Promise((resolve, reject) => {
        const fileReader = new FileReader();
        fileReader.readAsDataURL(file);

        fileReader.onload = () => {
            resolve(fileReader.result);
        };

        fileReader.onerror = (error) => {
            reject(error);
        };
    });
  };

Maybe is some setting somewhere where you have to set on client side, there is a setting for axios for example where you can set that limit for the client application.

You can try with axios directly too to make that request.

Id you try it directly in docs interface then it works with 100kb?

sorry, what do you mean by docs interface?

docs.moralis.io and there you have a tab for api reference and the you can run the api endpoints directly there

In your self hosted server’s /src/index.ts file, try changing:

app.use(express.json());

To something like:

app.use(express.json({ limit: '50mb' }));

And then rebuild (npm run build then npm run start).

2 Likes

I managed to solve this issue by adding
client_max_body_size
to nginx.conf file on AWS EC2

image

2 Likes

Is this with the express.json change or without? Any other changes? In case another user has this issue and they’re using Nginx.

First changing only express.json doesn’t work.Additionally changing nginx.conf works
Both are changed.

1 Like