Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

object` ("[object Response]") cannot be serialized as JSON?

I am trying to consume my api through code I am getting this error

object` ("[object Response]") cannot be serialized as JSON

But when I call or use this api through browser I am getting response.

here is my code https://codesandbox.io/s/naughty-platform-1xket?file=/pages/index.js

I am consuming my api like this

 console.log("-----");
  const cc = await fetch("https://1xket.sse.codesandbox.io/api/basecss/");
  console.log(cc, "llll");

API design

export default async (req, res) => {
  const stylesheet = await (
    await fetch("https://www.****.com/asset/web/css/***-base.css", {})
  ).text();
  console.log(stylesheet, "server");
  res.status(200).send(stylesheet);
};

I am getting this console value on server. but when I am calling this api through code I am getting this error

object` ("[object Response]") cannot be serialized as JSON. Please only return JSON serializable data types
like image 262
user944513 Avatar asked Mar 02 '23 19:03

user944513


1 Answers

That is because fetch returns a Response, after the response is returned, you need to parse the response response.json() response.text() etc. This is also an asynchronous operation, and I believe that you can't nest await statements like that.

  const response = await fetch("https://www.****.com/asset/web/css/***-base.css")
  const data = await response.text()

like image 63
Ivan V. Avatar answered Mar 12 '23 15:03

Ivan V.