Benni
Benni

Reputation: 654

How to fetch an API in NextJS from the api folder

I created an API with NextJS in the API folder. Now I want to fetch the data from that API in the getServerSideProps function.

const res = await fetch(`localhost:3000/api/playlist`);

But I get an error that says that it only supports HTTPS protocols.

Server Error

TypeError: Only HTTP(S) protocols are supported
This error happened while generating the page. Any console logs will be displayed in the terminal window.

How can I fetch the data from the NextJS API on my localhost? Thanks for you helping out!

Upvotes: 4

Views: 3609

Answers (1)

Benni
Benni

Reputation: 654

I found the problem, I forgot to add http:// in front of the URL.

The code would then look like this:

const res = await fetch(`http://localhost:3000/api/playlist`);

Upvotes: 2

Related Questions