'why am i not getting the data from Rapidapi
i want to use rapidapi to get some data using redux, but when i console.log the data i will be getting GET https://coinranking1.p.rapidapi.com/coins/coins 401 (Unauthorized). please what am i doing wrong, someone help me out
here is my code on my store.js file
import { configureStore } from "@reduxjs/toolkit"
import {cryptoApi} from "../services/cryptoApi"
export default configureStore({
reducer: {
[cryptoApi.reducerPath]: cryptoApi.reducer,
},
})
here is where i store the data am getting from rapidapi
import { createApi, fetchBaseQuery} from "@reduxjs/toolkit/query/react"
const cryptoApiHeaders = {
'X-RapidAPI-Host': 'coinranking1.p.rapidapi.com',
'X-RapidAPI-Key': my-key
}
const baseUrl = 'https://coinranking1.p.rapidapi.com/coins'
const createRequest = (url) => ({url, Headers: cryptoApiHeaders })
export const cryptoApi = createApi({
ReducerPath: "cryptoApi",
baseQuery: fetchBaseQuery( { baseUrl } ),
endpoints: (builder) => ({
getCryptos: builder.query({
query: () => createRequest("/exchanges")
})
})
})
export const { useGetCryptosQuery, } = cryptoApi;
import { useGetCryptosQuery } from "../services/cryptoApi"
const {data, isFetching } = useGetCryptosQuery();
console.log(data);
Solution 1:[1]
Instead of this:
const baseUrl = 'https://coinranking1.p.rapidapi.com/coins'
Try changing like this:
const baseUrl = 'https://coinranking1.p.rapidapi.com'
Also add coins or exchanges according to the data you need.
if you data from exchanges add this:
query: () => createRequest("/exchanges")
or if you need coins data change like this:
query: () => createRequest("/coins")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | nandanholla |