Next.js Discord

Discord Forum

Fetch failed on Strapi API Call

Answered
Masai Lion posted this in #help-forum
Open in Discord
Avatar
Masai LionOP
I am building a nextjs app that uses Strapi as its headless CMS. I added some content in the CMS, and the url works. Also, I use the correct api key. When in the NextJS app I attempt to do a fetch in a server side component (using App Router) I continously get the 'fetch failed' error. I am looking for a way to get some more insights what's going wrong. Next runs on localhost:3000 where the Strapi runs on localhost:1337.

How can I get this fetch fixed? I don't seem to do much differnt from the examples of Strapi
Answered by Masai Lion
I have just solved this by using 127.0.0.1 over localhost. Why this works, I have no idea yet, but it works.
View full answer

2 Replies

Avatar
Masai LionOP
I use the fetch as:
const response = await fetch('http://localhost:1337/api/articles', {
            headers: {
                'Authorization': `bearer ${process.env.STRAPI_API_KEY}`,
            }
        })
Avatar
Masai LionOP
I have just solved this by using 127.0.0.1 over localhost. Why this works, I have no idea yet, but it works.
Answer