Created
January 29, 2022 15:19
-
-
Save justinnoel/d3bbe3728e6eff4f57d1b926b8884f03 to your computer and use it in GitHub Desktop.
Get POST data submitted as either JSON or FormData
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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