-
Notifications
You must be signed in to change notification settings - Fork 27.2k
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
Middleware throws TypeError
when trying to parse FormData
#36497
Comments
TypeError
when trying to parse FormDataTypeError
when trying to parse FormData
Is anyone working on this? Can I? Thanks 🙏🏻 |
hey @nelodev, I am not sure if you have any progress with it, so I'm going to check this out 🙏 |
summary of #37980: a url encoded body does work, but multipart form data does not. it's blocked on undici/the edge-runtime library to implement this. 🙏 lets do it |
Is there any way to convert the x-www-form-urlencoded data to JSON data, similar to bodyParser.urlencoded? |
related: nodejs/undici#974 |
The latest Next.js canary should resolve this issue. In fact, I built a little demo at https://with-edge.vercel.app/api/form-data :✨ // pages/api/form-data.ts
import { NextRequest, NextResponse } from 'next/server'
export const config = { runtime: 'experimental-edge' }
/**
* Original source
* https://gist.github.com/eligrey/8335f09276492e69b747fb4017e9570e
*
* Get the cryptographic hash of an ArrayBuffer
*
* @param ab - ArrayBuffer to digest
* @param algorithm - Cryptographic hash digest algorithm
* @returns Hexadecimal hash digest string
*/
export const hash = async (
algorithm: string,
ab: ArrayBuffer,
): Promise<string> =>
new Uint8Array(await crypto.subtle.digest(algorithm, ab)).reduce(
(memo, i) => memo + i.toString(16).padStart(2, '0'),
'',
);
const ENDPOINT = process.env.NODE_ENV === 'production'
? 'https://with-edge.vercel.app'
: 'http://localhost:3000'
export default async function handler(req: NextRequest) {
if (req.method === 'GET') {
return NextResponse.json({
description: 'Upload a file as multipart.',
usage: `curl -X POST ${ENDPOINT}/api/form-data -F "file=@/logo.png" | jq`
})
}
const formData = await req.formData()
const file = formData.get('file') as File
const arrayBuffer = await file.arrayBuffer()
return new Response(JSON.stringify({
name: file.name,
type: file.type,
size: file.size,
sha256: await hash('SHA-256', arrayBuffer),
sha1: await hash('SHA-1', arrayBuffer),
sha384: await hash('SHA-384', arrayBuffer),
sha512: await hash('SHA-512', arrayBuffer)
}))
} Can you confirm is works for you using Next.js (if not, I will reopen the issue) |
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Verify canary release
Provide environment information
What browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
Describe the Bug
Not sure if I'm doing anything unexpected, but here goes:
request.formData()
- throwsWhile trying to parse
FormData
in a middleware like so:With the following request (
application/x-www-form-urlencoded
):or (
multipart/form-data
):curl --request POST \ --url http://localhost:3000/foo \ --header 'Content-Type: multipart/form-data; boundary=---011000010111000001101001' \ --form foo=bar
Next throws the following error:
request.json()
- worksMaking a similar request but with
JSON
, using therequest.json()
-method everything works as expected.Expected Behavior
request.formData()
to parse and return the data formultipart/form-data
andapplication/x-www-form-urlencoded
To Reproduce
pages/_middleware.ts
curl --request POST \ --url http://localhost:3000/foo \ --header 'Content-Type: multipart/form-data; boundary=---011000010111000001101001' \ --form foo=bar
The text was updated successfully, but these errors were encountered: