I'm fetching an API from the OMDB API and I get this output (TypeError: Failed to fetch)
.
I tested the API with Postman and I got a Positive response. My App has a inputbox and a search button only! It should display the movies when a user inputs a title.
class App extends React.Component {
//state that stores the movies fetched from imdb database
state = {
movies: []
}
//sendRequest function that uses unirest to get our movie information.
sendRequest = (Title) => {
const req = unirest("GET", "http://www.omdbapi.com/?i=tt3896198&apikey=275a2eec");
req.query({
"page": "1",
"r": "json",
"s": Title
});
req.headers({
"x-rapidapi-host": "The OMDb API",
"x-rapidapi-key": "275a2eec"
});
req.end((res) => {
if (res.error) throw new Error(res.error);
//We’re pulling our movies out of the response data, and storing them in our app state.
const movies = res.body.Search;
this.setState({
movies
});
console.log(res.body);
});
}
}