nitro
nitro copied to clipboard
cannot read form data with `readFormData` on vercel edge
Moving from https://github.com/unjs/nitro/discussions/1718 reported by @cosbgn
Minimal reproduction:
routes/index.ts:
export default defineEventHandler(async (event) => {
if (event.method === "POST") {
return await readFormData(event)
.then((r) => r.get("name"))
.catch((e) => e.stack);
}
return `
<script type="module">
console.log('Fetching...')
const data = new FormData();
data.append("name", "John Doe");
const res = await fetch("/", { method: 'POST', body: data });
const text = await res.text();
console.log('Fetch complete: ' + text)
alert(text)
</script>
<h1>Check Console</h1>
`;
});
(checking console, response never handles)
Workaround
You can use the older readMultipartFormData
utility. It returns an array of form datas:
export default defineEventHandler(async (event) => {
if (event.method === "POST") {
return await readMultipartFormData(event)
.then((data) =>
data.find((d) => d.name === "name")?.data.toString("utf8")
)
.catch((e) => e.stack);
}
return `
<script type="module">
console.log('Fetching...')
const data = new FormData();
data.append("name", "John Doe");
const res = await fetch("/", { method: 'POST', body: data });
const text = await res.text();
console.log('Fetch complete: ' + text)
alert(text)
</script>
<h1>Check Console</h1>
`;
});
https://nitro-ppspi2qf2-pi0.vercel.app/