Browse Source

feat: support `multipart/form-data` in HTTP request block (#735)

Ahmad Kholid 2 years ago
parent
commit
555797b016
2 changed files with 14 additions and 0 deletions
  1. 1 0
      src/utils/shared.js
  2. 13 0
      src/utils/webhookUtil.js

+ 1 - 0
src/utils/shared.js

@@ -1351,6 +1351,7 @@ export const excludeOnError = [
 
 export const contentTypes = [
   { name: 'application/json', value: 'json' },
+  { name: 'multipart/form-data', value: 'form-data' },
   { name: 'application/x-www-form-urlencoded', value: 'form' },
 ];
 

+ 13 - 0
src/utils/webhookUtil.js

@@ -8,6 +8,18 @@ const renderContent = (content, contentType) => {
   if (contentType === 'application/x-www-form-urlencoded') {
     return new URLSearchParams(renderedJson);
   }
+  if (contentType === 'multipart/form-data') {
+    if (!Array.isArray(renderedJson) || !Array.isArray(renderedJson[0])) {
+      throw new Error('The body must be 2D Array');
+    }
+
+    const formData = new FormData();
+    renderedJson.forEach((data) => {
+      formData.append(...data);
+    });
+
+    return formData;
+  }
 
   return JSON.stringify(renderedJson);
 };
@@ -27,6 +39,7 @@ const filterHeaders = (headers) => {
 
 const contentTypes = {
   json: 'application/json',
+  'form-data': 'multipart/form-data',
   form: 'application/x-www-form-urlencoded',
 };
 const notHaveBody = ['GET', 'DELETE'];