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: Some HTTP requests sent by apps don't have their data parsed into JSON #30560

Merged
merged 4 commits into from
Oct 12, 2023
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
5 changes: 5 additions & 0 deletions .changeset/stale-masks-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rocket.chat/server-fetch': patch
---

Fixed an issue where the payload of an HTTP request made by an app wouldn't be correctly encoded in some cases
30 changes: 9 additions & 21 deletions packages/server-fetch/src/parsers.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,20 @@
import type { ExtendedFetchOptions, FetchOptions, OriginalFetchOptions } from './types';

function isPostOrPutOrDeleteWithBody(options?: ExtendedFetchOptions): boolean {
// No method === 'get'
if (!options?.method) {
return false;
}
const { method, body } = options;
const lowerMethod = method?.toLowerCase();
return ['post', 'put', 'delete'].includes(lowerMethod) && body != null;
}

d-gubert marked this conversation as resolved.
Show resolved Hide resolved
const jsonParser = (options: ExtendedFetchOptions) => {
if (!options) {
return {};
}

if (isPostOrPutOrDeleteWithBody(options)) {
try {
if (options && typeof options.body === 'object' && !Buffer.isBuffer(options.body)) {
options.body = JSON.stringify(options.body);
options.headers = {
'Content-Type': 'application/json',
...options.headers,
};
}
} catch (e) {
// Body is not JSON, do nothing
try {
if (typeof options.body === 'object' && !Buffer.isBuffer(options.body)) {
options.body = JSON.stringify(options.body);
options.headers = {
...options.headers,
'Content-Type': 'application/json', // force content type to be json
};
}
} catch (e) {
// Body is not JSON, do nothing
}

return options as FetchOptions;
Expand Down
Loading