Skip to content
Myr

fix(request): serialize cached JSON body in cloneRawRequest (#5288)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Yusuke Wada · committed by GitHub · parent 612b59c · Browse files · 2 files+23 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+23 added−4 removed

  • tests:+18 added−0 removed
  • other:+5 added−4 removed

2 files+23 added−4 removed

modified:src/request.test.ts+18 added
619 unmodified lines
620 expect(clonedReq.headers.has('Content-Length')).toBe(false)621 expect((await clonedReq.formData()).get('foo')).toBe('bar')622 })623624 test('clones request when external code populated bodyCache.json', async () => {625 const req = new HonoRequest(626 new Request('http://localhost', {627 method: 'POST',628 headers: {629 'Content-Type': 'application/json',630 },631 body: JSON.stringify({ foo: 'bar' }),632 })633 )634 await req.raw.json()635 req.bodyCache.json = Promise.resolve({ foo: 'bar' })636637 const clonedReq = await cloneRawRequest(req)638639 expect(await clonedReq.json()).toEqual({ foo: 'bar' })640 })641642 test('clones GET request without body', async () => {643 const req = new HonoRequest(644 new Request('http://localhost', {645 method: 'GET',
54 unmodified lines
modified:src/request.ts+5 added-4 removed
480 unmodified lines
481 'Cannot clone request: body was already consumed and not cached. Please use HonoRequest methods (e.g., req.json(), req.text()) instead of consuming req.raw directly.',482 })483 }484485 const body = await req[cacheKey]()485 let body: BodyInit = await req[cacheKey]()486 const headers = req.header()487 if (body instanceof FormData) {488 // The FormData is re-serialized, so the original content headers no longer489 // describe the body. Let the runtime generate them for the new representation.487 if (cacheKey === 'json') {488 body = JSON.stringify(body)489 delete headers['content-length']490 } else if (body instanceof FormData) {491 delete headers['content-type']492 delete headers['content-length']493 }494
17 unmodified lines