Next.js: Formidable and form packages

Created on 14 Jul 2019  路  9Comments  路  Source: vercel/next.js

Bug report

Describe the bug

I was on Next.js v8 with a monorepo, but am now upgrading to v9 with all endpoints moved to the new pages/api/ feature. But now any of my post endpoints that include file uploading do not work. I tried to simplify the reproduction as much as possible, not even worrying about the API endpoint resolving.

The problem is that the Formidable parse never seems to finish. I also tried Tim鈥檚 urlencoded-body-parser as alternative, but it seems to hang also.

A clear and concise description of what the bug is.

To Reproduce

Micro endpoint in pages/api/endpoint.js:

const { run } = require("micro");
const formidable = require("formidable");

const endpoint = async (req, res) => {
  const data = await new Promise(function(resolve, reject) {
    const form = new formidable.IncomingForm();

    form.parse(req, function(err, fields, files) {
      if (err) return reject(err);
      resolve({ fields, files });
    });
  });
};

module.exports = (req, res) => run(req, res, endpoint);

Client-side post in pages/index.js:

const response = await fetch(url, {
  body,
  credentials: "include",
  headers: {
    Authorization: JSON.stringify({ token })
  },
  method: "POST"
});

Expected behavior

That the parse would finish and data would be assigned.

Screenshots

Screen Shot 2019-07-13 at 5 10 11 PM

System information

  • OS: macOS
  • Browser: Chrome
  • Version of Next.js: 9.0.1

Additional context

Add any other context about the problem here.

Most helpful comment

i think you need to add to pages/api/endpoint.js:

export const config = {
  api: {
    bodyParser: false,
  },
};

All 9 comments

i think you need to add to pages/api/endpoint.js:

export const config = {
  api: {
    bodyParser: false,
  },
};

Thanks a bunch @sync! My frustration is deserved due to not reading the docs.

If you use micro@canary you can do this:

const micro = require("micro");
const formidable = require("formidable");

async function endpoint (req, res) {
  const data = await new Promise(function(resolve, reject) {
    const form = new formidable.IncomingForm();

    form.parse(req, function(err, fields, files) {
      if (err) return reject(err);
      resolve({ fields, files });
    });
  });
};

export const config = {
  api: {
    bodyParser: false,
  },
};

export default micro(endpoint);

@sync Thank you! where can i find the documentation for the config object?

you should ask @timneutkens :-)

Docs are here: https://github.com/zeit/next.js#api-middlewares

If you use micro@canary you can do this:

const micro = require("micro");
const formidable = require("formidable");

async function endpoint (req, res) {
  const data = await new Promise(function(resolve, reject) {
    const form = new formidable.IncomingForm();

    form.parse(req, function(err, fields, files) {
      if (err) return reject(err);
      resolve({ fields, files });
    });
  });
};

export const config = {
  api: {
    bodyParser: false,
  },
};

export default micro(endpoint);

I can't get this working. I hit error resolver is not a function

i think you need to add to pages/api/endpoint.js:

export const config = {
  api: {
    bodyParser: false,
  },
};

You saved my days =.=

If you use micro@canary you can do this:

const micro = require("micro");
const formidable = require("formidable");

async function endpoint (req, res) {
  const data = await new Promise(function(resolve, reject) {
    const form = new formidable.IncomingForm();

    form.parse(req, function(err, fields, files) {
      if (err) return reject(err);
      resolve({ fields, files });
    });
  });
};

export const config = {
  api: {
    bodyParser: false,
  },
};

export default micro(endpoint);

I can't get this working. I hit error resolver is not a function

You can remove micro if you host your project on Vercel, this should resolve the issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

knipferrc picture knipferrc  路  3Comments

wagerfield picture wagerfield  路  3Comments

formula349 picture formula349  路  3Comments

olifante picture olifante  路  3Comments

swrdfish picture swrdfish  路  3Comments