No Errors for POST Formdata

i am trying to get my function to accept a http POST with formdata and then send that to a webhook to discord. Somehow i am only getting an empty object but also no errors in the logs in the dashboard :
/**
* POST /api/submit
*/
export async function onRequestPost(context) {
try {
let input = await context.request.formData();
let pretty = JSON.stringify([...input], null, 2);
console.log(input.toString());
console.log("Sending Discord Message");
sendDiscordMessage("Website Contact Form", pretty, "https://discord.com/api/webhooks/1161255461614526525/");
return new Response(pretty, {
headers: {
'Content-Type': 'application/json;charset=utf-8',
},
});
} catch (err) {
return new Response('Error parsing JSON content', { status: 400 });
}
}


async function sendDiscordMessage(name, message, webhookUrl) {
await fetch(webhookUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
username: 'Website Contact Form',
embeds: [{
title: 'New Message',
type: 'rich',
fields: [
{
name: 'Name',
value: name,
},
{
name: 'Message',
value: message,
}
]
}]
}),
});
console.log("Message Sent");
console.log(fetch)
}
/**
* POST /api/submit
*/
export async function onRequestPost(context) {
try {
let input = await context.request.formData();
let pretty = JSON.stringify([...input], null, 2);
console.log(input.toString());
console.log("Sending Discord Message");
sendDiscordMessage("Website Contact Form", pretty, "https://discord.com/api/webhooks/1161255461614526525/");
return new Response(pretty, {
headers: {
'Content-Type': 'application/json;charset=utf-8',
},
});
} catch (err) {
return new Response('Error parsing JSON content', { status: 400 });
}
}


async function sendDiscordMessage(name, message, webhookUrl) {
await fetch(webhookUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
username: 'Website Contact Form',
embeds: [{
title: 'New Message',
type: 'rich',
fields: [
{
name: 'Name',
value: name,
},
{
name: 'Message',
value: message,
}
]
}]
}),
});
console.log("Message Sent");
console.log(fetch)
}
1 Reply
JustinNoel
JustinNoel15mo ago
I "think" you need an await in front of sendDiscordMessage.
Want results from more Discord servers?
Add your server