"I can resolve this issue by using the CORS Chrome extension. However, in that case, the app will work only on localhost. It will not work after deployment.
API I am using: https://www.swiggy.com/dapi/restaurants/list/v5?lat=18.97210&lng=72.82460&is-seo-homepage-enabled=true&page_type=DESKTOP_WEB_LISTING
"
import RestaurantCard, { withPromotedLabel } from "./RestaurantCard";
import { useState, useEffect, useContext } from "react";
import Shimmer from "./Shimmer";
import { Link } from "react-router-dom";
import useOnlineStatus from "../utils/useOnlineStatus";
import UserContext from "../utils/UserContext";
const Body = () => {
const [listOfRestaurants, setListOfRestaurants] = useState([]);
const [filteredRestaurant, setFilteredRestaurant] = useState([]);
const [searchText, setSearchText] = useState("");
const RestaurantCardPromoted = withPromotedLabel(RestaurantCard);
useEffect(() => {
fetchData();
}, []);
const fetchData = async () => {
const data = await fetch(
"https://www.swiggy.com/dapi/restaurants/list/v5?lat=18.97210&lng=72.82460&is-seo-homepage-enabled=true&page_type=DESKTOP_WEB_LISTING"
);
const json = await data.json();
setListOfRestaurants(json?.data?.cards[4]?.card?.card?.gridElements?.infoWithStyle?.restaurants);
setFilteredRestaurant(json?.data?.cards[4]?.card?.card?.gridElements?.infoWithStyle?.restaurants);
};
const onlineStatus = useOnlineStatus();
if (onlineStatus === false) {
return (
<h1>
Looks like you're offline!! Please check your internet connection.
</h1>
);
}
const { loggedInUser, setUserName } = useContext(UserContext);
// Conditional Rendering
return listOfRestaurants.length === 0 ? <Shimmer /> : (
<div className="body">
<div className="filter flex">
<div className="search m-4 p-4">
<input
type="text"
data-testid="searchInput"
className="border border-solid border-black"
value={searchText}
onChange={(e) => {
setSearchText(e.target.value);
}}
/>
<button
className="px-4 py-2 bg-green-100 m-4 rounded-lg"
onClick={() => {
const filteredRestaurant = listOfRestaurants.filter(
(res) => res.info.name.toLowerCase().includes(searchText.toLowerCase())
);
setFilteredRestaurant(filteredRestaurant);
}}
>
Search
</button>
</div>
<div className="search m-4 p-4 flex items-center">
<button
className="px-4 py-2 bg-gray-100 rounded-lg"
onClick={() => {
const filteredList = listOfRestaurants.filter(
(res) => res.info.avgRating > 4
);
setFilteredRestaurant(filteredList);
}}
>
Top Rated Restaurants
</button>
</div>
<div className="search m-4 p-4 flex items-center">
<label className="p-2 pl-4">UserName:</label>
<input
className="border border-black p-2"
value={loggedInUser}
onChange={(e) => setUserName(e.target.value)}
/>
</div>
</div>
{/* Restaurant cards rendering */}
<div className="restaurant-list">
{filteredRestaurant.map((restaurant) => (
<Link to={`/restaurant/${restaurant.info.id}`} key={restaurant.info.id}>
{restaurant.info.veg ? (
<RestaurantCardPromoted resData={restaurant} />
) : (
<RestaurantCard resData={restaurant} />
)}
</Link>
))}
</div>
</div>
);
};
export default Body;
2
Answers
If the API is not for the public (Only for their company) and I checked there is no documentation related to it, I assumed you can’t use the API. But, if you want to make sure, you can use a proxy in
package.json
like this:Edit:
After I tried to hit the API from
Postman
, it returned403 Forbidden
and I assumed it meant the API was only for their companyThis is https://www.swiggy.com API, You can’t directly call from your domain,
To call This API,You have to use additional backend project of Node Or Express.
I have Created Node Project For It
https://codesandbox.io/p/devbox/wandering-http-fc498q?file=%2Findex.js%3A12%2C17