Skip to content
Myr

fix(request): `req.json()` keeps the content as is (#4269)

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 ed9d72f · Browse files · 2 files+13 added−1 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+13 added−1 removed

  • tests:+12 added−0 removed
  • other:+1 added−1 removed

2 files+13 added−1 removed

modified:src/request.test.ts+12 added
220 unmodified lines
221 })222 )223 })224225 test('req.json() should keep the content as is', async () => {226 const text = '{ "foo" : "bar" }'227 const req = new HonoRequest(228 new Request('http://localhost', {229 method: 'POST',230 body: text,231 })232 )233 expect(await req.json()).toEqual(JSON.parse(text))234 expect(await req.text()).toEqual(text)235 })236237 test('req.arrayBuffer()', async () => {238 const buffer = new TextEncoder().encode('{"foo":"bar"}').buffer239 const req = new HonoRequest(240 new Request('http://localhost', {
107 unmodified lines
modified:src/request.ts+1 added-1 removed
241 unmodified lines
242 * })243 * ```244 */245 json<T = any>(): Promise<T> {246 return this.#cachedBody('json')246 return this.#cachedBody('text').then((text: string) => JSON.parse(text))247 }248249 /**250 * `.text()` can parse Request body of type `text/plain`
169 unmodified lines