Last active
August 19, 2021 14:21
-
-
Save Sanjay007/1844e79b158582650f9f87002399393b to your computer and use it in GitHub Desktop.
How to Fetch Data From API in Next JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static getInitialProps = async function() { | |
const catreq = [ | |
"World", | |
"Country", | |
"Sports", | |
"Health", | |
"EntertaintMent", | |
"Business", | |
"Tech", | |
"Politics", | |
"State" | |
]; | |
const res = await fetch('https://api.tvmaze.com/search/shows?q=batman'); | |
const data = await res.json(); | |
const latestnews= await fetch(`http://quicknews360.com:8080/news/lateast`, { | |
headers: { | |
language: | |
typeof window !== "undefined" | |
? localStorage.getItem("language") != undefined | |
? localStorage.getItem("language") | |
: "English" | |
: "", | |
"Content-Type": "application/json" | |
}, | |
method: "get" | |
}); | |
const latestn=await latestnews.json(); | |
let reqbody = { category: catreq }; | |
const categoryData=await fetch(`http://quicknews360.com:8080/news/newsbycategory`, { | |
headers: { | |
language: | |
typeof window !== "undefined" | |
? localStorage.getItem("language") != undefined | |
? localStorage.getItem("language") | |
: "English" | |
: "", | |
"Content-Type": "application/json" | |
}, | |
method: "post", | |
body: JSON.stringify(reqbody) | |
}); | |
const catResp=await categoryData.json(); | |
//console.log(`Show data fetched. Count: ${data.length}`); | |
return { | |
shows: data.map(entry => entry.show), | |
latestnews:latestn, | |
category:catResp, | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment