I am trying to get Server Side Props by using Strapi API but I am getting an Error
Unanswered
Atlantic salmon posted this in #help-forum
Atlantic salmonOP
The API works fine in postman and is being hit on the server as well but when I call it from the frontend, I get this error
4 Replies
Atlantic salmonOP
This is the Error that I am getting
Error fetching data: TypeError: fetch failed
at Object.fetch (node:internal/deps/undici/undici:11118:11)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async getServerSideProps (webpack-internal:///./pages/products/index.jsx:132:26) {
cause: Error: connect ECONNREFUSED ::1:1337
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1300:16) {
errno: -4078,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '::1',
port: 1337
}
}This is how I am calling the api
export async function getServerSideProps(context) {
try {
const headers = {
Authorization: "<API key>",
};
const response = await fetch("http://localhost:1337/api/products?populate=*", { headers: headers });
if (!response.ok) {
throw new Error(`Request failed with status ${response.status}`);
}
const products = await response.json();
console.log(products);
return {
props: { products: products },
};
} catch (error) {
console.error("Error fetching data:", error);
return {
props: { products: null },
};
}
}When I call it using postman
Response in postman