I’m having trouble integrating RapidAPI into my React Native app. Previously I was fetching user information directly from a JSON endpoint on my server and everything worked fine:
async loadUserData() {
try {
const result = await fetch('myserver.com/userinfo.json');
const jsonData = await result.json();
this.setState({
loading: false,
userData: jsonData,
});
}
catch (err) {
console.log(err);
}
}
Now I want to use RapidAPI for better security but my app shows a blank screen. Here’s what I tried:
async loadUserData() {
try {
const result = await fetch('random-quotes1.p.rapidapi.com/quotes', {
"method": "GET",
"headers": {
"x-rapidapi-host": "myserver.com",
"x-rapidapi-key": 'abc123def456ghi789jkl012mno345pqr678stu901vwx234'
}
});
const jsonData = await result.json();
this.setState({
loading: false,
userData: jsonData,
});
}
catch (err) {
console.log(err);
}
}
The RapidAPI docs show this example:
const getData = () => {
beginRequest()
setIsLoading(true);
fetch('https://random-quotes1.p.rapidapi.com/quotes', {
"method": "GET",
"headers": {
"x-rapidapi-host": "random-quotes1.p.rapidapi.com",
"x-rapidapi-key": 'your-api-key'
}
})
.then((result) => result.json())
.then((response) => setQuoteData(response.quote))
.catch(() => Alert.alert('Error occurred', 'Could not retrieve quote data.'))
.finally(() => {
setIsLoading(false)
finishRequest()
});
};
What am I doing wrong with the RapidAPI integration?