fix(request): keep the request media type when reusing a cached body (#5366)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* fix(request): keep the request media type when reusing a cached body `HonoRequest` caches the parsed body so it can be read through more than one representation. The fallback that rebuilds a body from an already cached representation went through a bare `Response`: return new Response(body)[key]() A `Response` constructed that way has no `Content-Type`, so a representation that needs the media type could no longer be produced even though the bytes were still available: app.post('/', async (c) => { const raw = await c.req.text() // e.g. verify a signature first const form = await c.req.formData() // TypeError }) For an `application/x-www-form-urlencoded` or `multipart/form-data` request the second call threw `TypeError: Content-Type was not one of ...`. `c.req.blob()` was unaffected (a `Blob` carries its own type) and `c.req.parseBody()` was unaffected (it reads the media type from `req.raw.headers`), so the same app behaved differently depending on which accessor was used first. Carry the original `Content-Type` over when rebuilding the body. `FormData` is left alone, because `Response` has to generate a fresh multipart boundary for it. * refactor(request): check the cache key instead of instanceof FormData ---------
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: Xia Chao <236466140+bun-unsafe@users.noreply.github.com>
- Co-authored-by: Yusuke Wada <yusuke@kamawada.com>
Diff stats
2 files+62 added−1 removed
- tests:+52 added−0 removed
- other:+10 added−1 removed