mirror of
https://github.com/honojs/hono.git
synced 2024-12-01 11:51:01 +01:00
387d696303
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.
19 lines
491 B
TypeScript
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
|
|
}
|