-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RTK Query - Setting headers doesn't work #2205
Comments
addUser: builder.mutation({
query: (data) => {
return {
url: `auth/signup`,
method: "POST",
body: data
};
},
}), should be perfectly fine, assuming you put a normal JavaScript object and not some class instance in there. As for why your |
Passing the body object like that still won't work too, I was passing it like that at first, what I'm trying to achieve is sending a POST request for registering a user and I'm using supertokens library for auth and I had an issue with that so after searching they said I need to set the 'content-type' of my request to 'application/json' , this is what I'm trying to achieve right now with RTK Query. This is how I'm creating the body object
It still won't work, I have also tried deleting the npm_modules folder and reinstalling everything but still the same issue. |
Well, all I can say is that this should work. You can verify it in https://codesandbox.io/s/github/reduxjs/redux-toolkit/tree/master/examples/query/react/authentication-with-extrareducers?from-embed=&file=/src/app/services/auth.ts for example. We don't do anything different there. My only other idea would be that you have CORS problems, but then you should have very big red warnings on your browser console about that. |
This is very odd but I'll keep digging, thanks anyway! |
I am also having a very similar issue with SuperTokens + RTK Query ( I think the problem lies with the way SuperTokens interacts w/
A trivial/suboptimal workaround for right now is to circumvent
|
@anmilleriii that looks like that "modified fetch" just does not support getting passed a The first argument to Supertokens fails to implement the standard there. |
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 |
I have a similar problem to @mohamedamara1's.
The requested format of |
@lnicepei are you using Generally: the only content-type that RTKQ+fetchBaseQuery would ever set is |
In that case, this is browser behaviour and has nothing to do with RTK Query. RTK Query just calls Generally: CORS problems like this have to be solved at the server side - the server has to send headers that indicate that a requerst of this kind is allowed. |
Thank you so much(!) |
Hello, having a smiliar issue with POST/PATCH/PUT. However this is not caused by any cors settings (have tested). Using the rtk code gen
|
I have similar problem with most of you guys, I solved it by enabling the server CORS, by default |
Maybe I missed something, but the request doesn't have that header at all, all the requests were working fine with axios and fetch (get). Also, cors is enabled on the server already. |
@dmccoy-NL I have been struggling with the same issue all morning. Like you, my fetch (post) and axios requests are working fine but fetchBaseQuery post requests are missing the Content-Type header. Like you, I also tried setting the header myself from the Have you had any luck? (I had to edit this comment, I had thought I had gotten it working but was incorrect... my code was still calling plain old fetch() in the working version...) |
If any of those can create a reproduction (keep in mind, we need a client and server for this!) we can give feedback. |
Even in this example, headers are not being set. Please have a look in browser network tab and try to find the authorization header |
hi, could someone solve it? I'm stuck with this when sending a header Authorization sends it to me in lowercase |
@lucaspieran HTTP headers are per standard case-insensitive. HTTP2 even enforces them to be lowercase. |
@phryneas oh i see, that is to say that the lowercase cannot be solved? |
Headers being lowercase is a part of the standard. There's nothing to be solved, they should always be compared case-insensitive and best treated lowercase. If you expect them to be non-lowercase on the server, you go against HTTP standards. Also, it's not RTKQ that makes them lowercase. Your browser is. |
I too am having this issue, not sure why the headers are not setting. I am using NextJS.
This function is running and it's fetching the query token but it's not appearing on the server. I checked the request headers and its missing from it, there are many other headers there.
|
99% it's CORS. Does the server declare to the browser that the browser is allowed to send these headers? Do you see an error message? |
I checked all of the cors issues, it wasn't that. I have fixed it though, turns out it is my own code. Basically, I changed the prepareHeaders to an async function. The function was returning the value before the promise had been resolved.
Thank you for your help! :) |
Posting an update here... after struggling back in March, I've found the solution for my specific case. I work on a micro frontend, and it turns out that the framework it runs in implemented a wrapper around fetch that was implemented incorrectly. As phryneas notes above,
In our case, the fetch wrapper was overwriting any headers in the Request class instance that RTK Query passes to fetch. It was very similar to the issue caused by the supertokens library discussed earlier in this thread. To anyone else experiencing this issue in the future... as suggested by phryneas, check for a fetch wrapper/polyfill! It is easy to implement this incorrectly such that the headers get dropped/overwritten! |
I'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
it still won't work, btw console.log(headers) only returns an empty Headers object .
I also tried to set the header like this
I still can't see the 'content-type' header when checking it using browser dev tools.
The text was updated successfully, but these errors were encountered: