ofetch icon indicating copy to clipboard operation
ofetch copied to clipboard

Failed to parse response with `json` on `ofetch.raw`

Open becem-gharbi opened this issue 1 year ago • 1 comments

Environment

  • Operating System: Linux
  • Node Version: v18.18.0
  • Nuxt Version: 3.9.3
  • CLI Version: 3.10.0
  • Nitro Version: 2.8.1
  • Package Manager: [email protected]
  • Builder: -
  • User Config: devtools
  • Runtime Modules: -
  • Build Modules: -

Reproduction

https://stackblitz.com/edit/github-ojv4rk?file=app.vue

Describe the bug

When using ofetch.raw the raw response object cannot be parsed with json method (this works well with ofetch.native) . The failure happends both on client and server side thus I think it's not related to Node.

Additional context

I'm using @iconify/vue library. It provides setFetch method to use a custom fetch API. I have set fetch.raw in order benefit from the interceptors feature. The response on fetching icons is JSON thus it is parsed with json method.

Logs

### Node

TypeError: Body is unusable
    at specConsumeBody (https://rlmilwjxkgithub-cdbi.w-credentialless.staticblitz.com/blitz.6854296d.js:194:73015)
    at $.json (https://rlmilwjxkgithub-cdbi.w-credentialless.staticblitz.com/blitz.6854296d.js:194:74238)
    at eval (file:///home/projects/rlmilwjxk.github/app.vue:22:74)
    at Module.withAsyncContext (file:///home/projects/rlmilwjxk.github/node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:3366:19)
    at setup (file:///home/projects/rlmilwjxk.github/app.vue:22:47)

### Browser

TypeError: Failed to execute 'json' on 'Response': body stream already read

becem-gharbi avatar Jan 26 '24 09:01 becem-gharbi

https://github.com/unjs/ofetch/blob/244fb6a720478a8e834f4e2715f8cef866802440/src/fetch.ts#L188

response._data is what you want.

Lpmvb avatar Feb 23 '24 02:02 Lpmvb