Skip to content
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

Fix fetchJSON adds useless Content-Type header for empty requests #9613

Merged
merged 5 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 50 additions & 3 deletions packages/ra-core/src/dataProvider/fetch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,25 @@ describe('flattenObject', () => {
});

describe('createHeadersFromOptions', () => {
it('should add a Content-Type header for POST requests', () => {
const options = {
it('should add a Content-Type header for POST requests if there is a body', () => {
const optionsWithBody = {
method: 'POST',
body: JSON.stringify(null),
};

const headers = createHeadersFromOptions(options);
const headers = createHeadersFromOptions(optionsWithBody);
expect(headers.get('Content-Type')).toStrictEqual('application/json');
});

it('should not add a Content-Type header for POST requests with no body', () => {
const optionsWithoutBody = {
method: 'POST',
};

const headersWithoutBody = createHeadersFromOptions(optionsWithoutBody);
expect(headersWithoutBody.get('Content-Type')).toBeNull();
chrisDeFouRire marked this conversation as resolved.
Show resolved Hide resolved
});

it('should not add a Content-Type header for GET requests', () => {
const optionsWithoutMethod = {};
const optionsWithMethod = {
Expand All @@ -85,4 +95,41 @@ describe('createHeadersFromOptions', () => {
);
expect(headersWithoutMethod.get('Content-Type')).toBeNull();
});

it('should not add a Content-Type header for DELETE requests with no body', () => {
const optionsWithDelete = {
method: 'DELETE',
};

const headersWithDelete = createHeadersFromOptions(optionsWithDelete);
expect(headersWithDelete.get('Content-Type')).toBeNull();
const optionsWithDeleteAndBody = {
method: 'DELETE',
body: JSON.stringify(null),
};

const headersWithDeleteAndBody = createHeadersFromOptions(
optionsWithDeleteAndBody
);
expect(headersWithDeleteAndBody.get('Content-Type')).toStrictEqual(
'application/json'
);
});

it('should not add a Content-Type header if there already is a Content-Type header', () => {
const optionsWithContentType = {
headers: new Headers({
chrisDeFouRire marked this conversation as resolved.
Show resolved Hide resolved
'Content-Type': 'not undefined',
}) as Headers,
method: 'POST',
body: 'not undefined either',
};

const headersWithContentType = createHeadersFromOptions(
optionsWithContentType
);
expect(headersWithContentType.get('Content-Type')).toStrictEqual(
'not undefined'
);
});
});
14 changes: 9 additions & 5 deletions packages/ra-core/src/dataProvider/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ export const createHeadersFromOptions = (options: Options): Headers => {
new Headers({
Accept: 'application/json',
})) as Headers;
if (
!requestHeaders.has('Content-Type') &&
!(options && (!options.method || options.method === 'GET')) &&
!(options && options.body && options.body instanceof FormData)
) {
const hasBody = options && options.body;
const isContentTypeSet = requestHeaders.has('Content-Type');
const isGetMethod = !options?.method || options?.method === 'GET';
const isFormData = options?.body instanceof FormData;

const shouldSetContentType =
hasBody && !isContentTypeSet && !isGetMethod && !isFormData;
if (shouldSetContentType) {
requestHeaders.set('Content-Type', 'application/json');
}

if (options.user && options.user.authenticated && options.user.token) {
requestHeaders.set('Authorization', options.user.token);
}
Expand Down
Loading