Hi all. I want to use rtk query to get data from CatsApi. But I’ve tried a lot of options to add a header and none succeed. Who knows how to insert api key into the header using rtk query? Here is an example of my code.
I assume the ‘Api key’ string is just a placeholder, and you’ve tried using your own in its place. If so then looking at their docs your code looks good.
Depending on your local env it could potentially be a CORS issue, or something with the default headers - if so then this might catch it.
baseQuery: fetchBaseQuery({
baseUrl: '.....',
prepareHeaders: (headers, { getState }) => {
headers.set('x-api-key', `YOUR-KEY-HERE`)
headers.set('Content-Type', 'application/json')
headers.set('Access-Control-Allow-Origin', '*')
return headers
}
}),