[wrangler:err] TypeError: Parsing a Body as FormData requires a Content-Type header.

I believe this error is happening at request.formData() below and I'm just trying process the request from a basic html form with a few input fields and has theapplication/x-www-form-urlencoded content-type header, so I'm not sure why its throwing the error
export default {
async fetch(request, env, ctx) {
await request.formData().then(async (formData) => {
console.log(formData);
});
return new Response("Message sent successfully!");
}
};
export default {
async fetch(request, env, ctx) {
await request.formData().then(async (formData) => {
console.log(formData);
});
return new Response("Message sent successfully!");
}
};
Any advice would be appreciated.
2 Replies
Walshy
Walshy2d ago
$ curl https://worker-test.walshy.dev/urlencoded-form-data -X POST -d 'key=value' -s | jq .
{
"key": "value"
}
$ curl https://worker-test.walshy.dev/urlencoded-form-data -X POST -d 'key=value' -s | jq .
{
"key": "value"
}
cannot repro, looks fine to me
if (path === '/urlencoded-form-data') {
const formData = await req.formData();

return Response.json(Object.fromEntries(formData.entries()));
}
if (path === '/urlencoded-form-data') {
const formData = await req.formData();

return Response.json(Object.fromEntries(formData.entries()));
}
af
afOP2d ago
Thank you. I think it no longer throws the error after I use fetch in the client side JS instead of relying on the action attribute to submit the form.

Did you find this page helpful?