question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RTK Query - Setting headers doesn't work

See original GitHub issue

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

// 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.

image

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
anmilleriiicommented, Apr 10, 2022

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.

2reactions
phryneascommented, Apr 10, 2022

@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 a string, or a Request class instance.

Supertokens fails to implement the standard there.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found