Skip to content

Instantly share code, notes, and snippets.

@Sanjay007
Last active August 19, 2021 14:21
Show Gist options
  • Save Sanjay007/1844e79b158582650f9f87002399393b to your computer and use it in GitHub Desktop.
Save Sanjay007/1844e79b158582650f9f87002399393b to your computer and use it in GitHub Desktop.
How to Fetch Data From API in Next JS
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