fix: Some HTTP requests sent by apps don't have their data parsed into JSON (#30560)

pull/30614/head^2
Douglas Gubert 2 years ago committed by GitHub
parent 389bac8275
commit 35363420f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      .changeset/stale-masks-learn.md
  2. 30
      packages/server-fetch/src/parsers.ts

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

@ -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;
}
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;

Loading…
Cancel
Save