Skip to content

Instantly share code, notes, and snippets.

@justinnoel
Created January 29, 2022 15:19
Show Gist options
  • Save justinnoel/d3bbe3728e6eff4f57d1b926b8884f03 to your computer and use it in GitHub Desktop.
Save justinnoel/d3bbe3728e6eff4f57d1b926b8884f03 to your computer and use it in GitHub Desktop.
Get POST data submitted as either JSON or FormData
export async function getPostData(request) {
const {headers} = request;
const contentType = headers.get("content-type") || "";
if (contentType.includes("application/json")) {
return await request.json();
}
if (contentType.includes("form")) {
const formData = await request.formData();
const body = {};
for (const entry of formData.entries()) {
body[entry[0]] = entry[1];
}
return body
}
return undefined;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment