RTK Query - Setting headers doesn't work
See original GitHub issueI’m trying to send a POST request and I need to set the ‘content-type’ header to ‘application/json’, I’m using fetchBaseQuery and it’s supposed to automatically do that but it’s not working because when I check the sent request using Chrome Dev Tools and checking the request headers I don’t see ‘content-type’ set to ‘application/json’. I don’t see it at all so I tried to manually do that by either using prepareHeaders like this
// Or from '@reduxjs/toolkit/query' if not using the auto-generated hooks
import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";
// initialize an empty api service that we'll inject endpoints into later as needed
export const mainApi = createApi({
reducerPath: "mainApi",
baseQuery: fetchBaseQuery({
baseUrl: "http://localhost:5000/api/v1/",
prepareHeaders: (headers) => {
headers.set("Content-Type", "application/json");
console.log(headers);
return headers;
},
}),
endpoints: () => ({}),
});
it still won’t work, btw console.log(headers) only returns an empty Headers object . I also tried to set the header like this
addUser: builder.mutation({
query: (data) => {
// console.log(typeof body);
return {
url: `auth/signup`,
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify(data),
};
},
}),
I still can’t see the ‘content-type’ header when checking it using browser dev tools.
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:7 (3 by maintainers)
Top Results From Across the Web
fetchBaseQuery - Redux Toolkit
fetchBaseQuery is a factory function that generates a data fetching method compatible with RTK Query's baseQuery configuration option.
Read more >RTK Query not passing headers - Stack Overflow
The problem is that access token is unavailable when the query starts Make sure that adding the token executes before making queries.
Read more >Redux toolkit headers : r/reactjs - Reddit
Assuming you're talking about RTK Query, normally you'd define a prepareHeaders option in your fetchBaseQuery call, or pass a headers field ...
Read more >Creating React Apps With Redux Toolkit and RTK Query - Toptal
This article demonstrates how developers can use Redux with React Query-like functionality thanks to RTK Query, the latest addition to Redux ...
Read more >Redux Toolkit Query Set Token in Headers #9 [Urdu/Hindi]
reduxtoolkitquery #reduxtoolkitRedux Toolkit Query Tutorials, in this video, we are going to store the token in the redux store and access ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thanks for the quick response @phryneas!
I think that your comment here correctly identified this issue (and clarified that it pertains to SuperTokens, not RTK Query).
Therefore I would consider this issue out of scope to RTK query and closable here, (fixing the SuperTokens implementation of their
fetch
wrapper is now covered by this issue.@anmilleriii that looks like that “modified fetch” just does not support getting passed a
Request
object as the first argument.The first argument to
fetch
has to be a RequestInfo which is either astring
, or aRequest
class instance.Supertokens fails to implement the standard there.