0
0
mirror of https://github.com/honojs/hono.git synced 2024-12-01 11:51:01 +01:00
hono/deno_dist/utils/body.ts
Kelly Littlepage 387d696303
fix: application/x-www-form-urlencoded decoding (#1189)
The current logic for decoding application/x-www-form-urlencoded is
overly restrictive and misses `Content-Type` headers with a character
encoding set, i.e., application/x-www-form-urlencoded; charset=UTF-8.

This fix harmonizes the logic for handling multipart/form-data
and application/x-www-form-urlencoded in the `parseBody` method of
request objects.
2023-06-21 22:51:59 +09:00

19 lines
491 B
TypeScript

export type BodyData = Record<string, string | File>
export async function parseBody(r: Request | Response) {
let body: BodyData = {}
const contentType = r.headers.get('Content-Type')
if (
contentType &&
(contentType.startsWith('multipart/form-data') ||
contentType.startsWith('application/x-www-form-urlencoded'))
) {
const form: BodyData = {}
;(await r.formData()).forEach((value, key) => {
form[key] = value
})
body = form
}
return body
}