nuxt / http

Universal HTTP Module for Nuxt.js
https://http.nuxtjs.org
MIT License
229 stars 51 forks source link

onResponse hook: How to decode response.body #167

Closed lapwat closed 3 years ago

lapwat commented 3 years ago

In the onResponse hook, response.body is a ReadableStream, how to decode it ?

$http.onResponse((req, options, res) => {
  console.log('Response data :', res.body)
});
Response data : ReadableStream {locked: false}

I tried to read the stream but I get a byte array.

$http.onResponse(async (req, options, response) => {
  console.log('Response data :', await response.body.getReader().read())
});
Response data : {value: Uint8Array(296), done: false}
lapwat commented 3 years ago

Found the solution:

$http.onResponse(async (req, options, response) => {
  console.log('Response data :', await response.json())
})
Response data : { myAttr: "myValue" }