RTK-Query: Overriding header set in `prepareHeaders`
See original GitHub issueI’m setting the "Content-Type", "application/json" header for my endpoints in prepareHeaders.
baseQuery: fetchBaseQuery({
prepareHeaders: (headers) => {
headers.set("Content-Type", "application/json");
return headers;
},
}),
For a mutation declared in this createApi slice, I’m setting headers to multipart/form-data
query: ({ formData }) => ({
url: /setting,
method: "PUT",
headers: {
"content-type": "multipart/form-data"
},
body: formData
})
However, the request is failing because the content-type is still being set to application/json for this mutation. Is there any way to use prepareHeaders to set a global content-type and still be able to set a different content-type for individual endpoints?
I know that the documentation says that the content-type will automatically be set to application/json with no need to use prepareHeaders, however that’s not the behavior I’m seeing. When the content-type header isn’t set on a request, it’s left out of the request headers.
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
fetchBaseQuery - Redux Toolkit
Allows you to inject headers on every request. You can specify headers at the endpoint level, but you'll typically want to set common...
Read more >How to add headers to endpoints in RTK-Query Plugin?
I'm essentially trying to add a certain header based on the params of an api call, but am clueless how to configure the...
Read more >fetchBaseQuery - RTK Query
You can specify headers at the endpoint level, but you'll typically want to set common ... A fetch function that overrides the default...
Read more >Confused about accessing rtk-query data in prepareHeaders()
When my application starts, it will load configuration . This configuration will contain stuff like baseUrl, some data used in headers etc. I've ......
Read more >Creating React Apps With Redux Toolkit and RTK Query - Toptal
RTK Query is an advanced data-fetching and client-side caching tool. ... after initialization we will need to set up store configuration:
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
OK, that’s manageable. There was a line in the documentation that made me think that the priority in setting headers was inverse, with
prepareHeaderssetting a base header and individual endpoints being able to modify the header as needed.A workaround for the same can be to check whether a header is already set in the
prepareHeadersfunction itself. Theheadersvariable passed in theprepareHeadershelps you access the headers which are already set. Using that you can conditionally add a header in theprepareHeadersfunction in the following way: