-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
RTK Query - Changing the default header from fetchBaseQuery not working #4669
Comments
I think your Priority here is
|
the problem is that i cannot set a default header. I tried to add // approch 1 - set default header to fetchBaseQuery and custom header to query -- not working
const baseQuery = fetchBaseQuery({
headers: { 'Content-Type': 'application/json'}, // low priority
....rest code
});
export const pageApi = api.injectEndpoints({
endpoints: (builder) => ({
uploadMediaPage: builder.mutation<
Partial<ExampleProps>,
{ _id?: string; file: File; type: string; }
>({
query: ({ _id, ...formData }) => {
let bodyFormData = new FormData();
bodyFormData.append('file', formData.file);
bodyFormData.append('type', formData.type);
return {
url: `http://localhost:5000/${_id}/upload/`,
method: 'POST',
body: bodyFormData,
headers: {
'Content-Type': 'multipart/form-data', // med priority
},
};
},
})
}) // approch 2 - remove custom header from query (set automatically) and set default to fetchBaseQuery -- not working
const baseQuery = fetchBaseQuery({
headers: { 'Content-Type': 'application/json'}, // low priority
...rest of the code
});
export const pageApi = api.injectEndpoints({
endpoints: (builder) => ({
uploadMediaPage: builder.mutation<
Partial<ExampleProps>,
{ _id?: string; file: File; type: string; }
>({
query: ({ _id, ...formData }) => {
let bodyFormData = new FormData();
bodyFormData.append('file', formData.file);
bodyFormData.append('type', formData.type);
return {
url: `http://localhost:5000/${_id}/upload/`,
method: 'POST',
body: bodyFormData,
headers: {
'Content-Type': 'multipart/form-data', // med priority
},
};
},
})
}) approch 3 -- working but I need to remove the default header for all endpoints
const baseQuery = fetchBaseQuery({
// headers: { 'Content-Type': 'application/json'}, // I need to remove the default header
...rest of the code
});
export const pageApi = api.injectEndpoints({
endpoints: (builder) => ({
uploadMediaPage: builder.mutation<
Partial<ExampleProps>,
{ _id?: string; file: File; type: string; }
>({
query: ({ _id, ...formData }) => {
let bodyFormData = new FormData();
bodyFormData.append('file', formData.file);
bodyFormData.append('type', formData.type);
return {
url: `http://localhost:5000/${_id}/upload/`,
method: 'POST',
body: bodyFormData
};
},
})
}) |
Hmm, might be a |
I asked chat gpt and he gave me a solution that works, but I don't know if is a good approch: const baseQuery = fetchBaseQuery({
prepareHeaders: (headers, { getState, endpoint, arg }) => {
if (arg && typeof arg === 'object' && 'body' in arg && arg.body instanceof FormData) {
// Remove Content-Type for FormData to let the browser set it
headers.delete('Content-Type');
} else {
// Set Content-Type to application/json for all other requests
headers.set('Content-Type', 'application/json');
}
return headers;
},
}); Inside |
Setting |
For almost all endopoints I need the
headers.set("Content-Type", "application/json")
but there are also some endpoints such as those for uploading files where I need to replaceContent-Type", "application/json
withContent-Type': 'multipart/form-data
.From what I read in other topics, the content-type is set automatically from FormData, but in my case is not working. I also tried to set it manually but still not working
The text was updated successfully, but these errors were encountered: