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

Fix formData code snippet in route handler docs #52532

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -553,7 +553,9 @@ import { NextResponse } from 'next/server'

export async function POST(request: Request) {
const formData = await request.formData()
return NextResponse.json({ formData })
const name = formData.get('name')
const email = formData.get('email')
return NextResponse.json({ name, email })
}
```

Expand All @@ -562,10 +564,14 @@ import { NextResponse } from 'next/server'

export async function POST(request) {
const formData = await request.formData()
return NextResponse.json({ formData })
const name = formData.get('name')
const email = formData.get('email')
return NextResponse.json({ name, email })
}
```

Since `formData` data are all strings, you may want to use [`zod-form-data`](https://www.npmjs.com/zod-form-data) to validate the request and retrieve data in the format you prefer (e.g. `number`).

### CORS

You can set CORS headers on a `Response` using the standard Web API methods:
Expand Down