#getting json response from $fetch.raw returns an error

1 messages · Page 1 of 1 (latest)

torn tinsel
#

I have route handler which makes an external api call but stuck with an error [nuxt] [request error] [unhandled] [500] The body has already been consumed..

Heres my code:

export default defineEventHandler(async (event) => {
  const body = await readBody(event);
  const csrfResponse = await $fetch.raw<any>(
    "http://localhost:4000/api/csrf_token"
  );

  const data = await csrfResponse.json();
});

The compiler points the error to line const data = await csrfResponse.json(); but I'm not sure why it happen since this line is the only one using the csrfResponse.

torn tinsel
#

I don't encounter the said error when calling the endpoint via fetch api

  const csrfResponse = await fetch("http://localhost:4000/api/csrf_token");

  const data = await csrfResponse.json();
marsh nova
#

Have you tried removing .json() when using $fetch? Looking at the ofetch docs even with raw ofetch deserializes JSON for you