next-runtime icon indicating copy to clipboard operation
next-runtime copied to clipboard

[Bug]: request.formData() on RouteHandler throws error

Open HubertHalim opened this issue 2 years ago • 1 comments

Summary

A POST request handler that takes in a request in the form of multipart/form-data cannot parse the request properly. According to NextJs docs, the request (https://developer.mozilla.org/en-US/docs/Web/API/Request) has a formData() function to parse the request object.

Calling this formData() function locally and on other hosting platforms like Railway and bluehost are working fine, it only throws error when i call it on netlify. There is also no issue on local instance.

I have no idea what the exact error is as i do not have access to the logs on netlify, but i can provide the steps to reproduce below.

The website and endpoint associated with the issue is as follow: https://brilliant-lollipop-876008.netlify.app/api/hello

Steps to reproduce

  1. Send a POST request to https://brilliant-lollipop-876008.netlify.app/api/hello with the content in multipart/form-data
  2. The response will be "fail"
  3. if you were to clone the repo and run it locally the response will be "succeed"

A link to a reproduction repository

https://github.com/HubertHalim/test-bug

Next Runtime version

4.34.0

Is your issue related to the app directory (beta)?

  • [X] Yes, I am using the app directory

More information about your build

  • [ ] I am building using the CLI
  • [ ] I am building using file-based configuration (netlify.toml)

What OS are you using?

None

Your netlify.toml file

`netlify.toml`
# Paste content of your `netlify.toml` file here

Your public/_redirects file

`_redirects`
# Paste content of your `_redirects` file here

Your next.config.js file

`next.config.js`
# Paste content of your `next.config.js` file here. Check there is no private info in there.

Builds logs (or link to your logs)

Build logs
# Paste logs here

Function logs

Function logs
# Paste logs here

.next JSON files

generated .next JSON files
# Paste file contents here. Please check there isn't any private info in them
# You can either build locally, or download the deploy from Netlify by clicking the arrow next to the deploy time.

HubertHalim avatar Apr 14 '23 08:04 HubertHalim

Per https://nextjs.org/docs/basic-features/typescript#api-routes API routes should use NextApiRequest for request portion - that type doesn't have formData() method on it.

The https://developer.mozilla.org/en-US/docs/Web/API/Request is type we get from fetch calls - it might be that actually that type should be used but current Next.js types tell otherwise.

Above doesn't mean it's not intended for .formData() (and others like that) to be available, but the crumbs are little messy at this point and it's not clear to me that it being available is actually purposeful given that the types don't reflect that?

The request that we currently are getting is Node's http.ClientRequest type ( https://nodejs.org/api/http.html#class-httpclientrequest ) and that one definitely doesn't implement .formData(). It also is produced using @vercel/node-bridge, so we were using utils provided by Vercel for compatibility. (tho that package was recently discontinued - https://github.com/vercel/vercel/pull/9745 and we should investigate if we can bring replacement if it exists or maybe we will need to roll our own eventually as that package will no longer receive any updates)

Technically we could attach "missing" methods on req in https://github.com/netlify/next-runtime/blob/main/packages/runtime/src/templates/getHandler.ts#L93-L100 probably, but I'm not sure if this is actually correct thing to do.

pieh avatar Apr 28 '23 12:04 pieh

@HubertHalim Can you confirm whether this is still occurring with v5 of the runtime and Next.js 13.5+? Otherwise, we'll close this issue since it's been over a year.

serhalp avatar Jun 04 '24 16:06 serhalp

I'm closing this since there hasn't been any activity in over a year. Feel free to start a new thread on https://answers.netlify.com/ if you need more support.

serhalp avatar Jun 12 '24 13:06 serhalp