From bd0ca27856cee8f8e35ce36e5fa2e4d5b6e8aabb Mon Sep 17 00:00:00 2001 From: Ben Carp Date: Sat, 17 Apr 2021 20:09:28 +0300 Subject: [PATCH 1/5] adding toFormData test --- test/specs/helpers/toFormData.spec.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 test/specs/helpers/toFormData.spec.js diff --git a/test/specs/helpers/toFormData.spec.js b/test/specs/helpers/toFormData.spec.js new file mode 100644 index 0000000000..f28471ece3 --- /dev/null +++ b/test/specs/helpers/toFormData.spec.js @@ -0,0 +1,18 @@ +var toFormData = require("../../../lib/helpers/toFormData"); + +describe("toFormData", function () { + it("Convert nested data object to FormDAta", function () { + var o = { + val: 123, + nested: { + arr: ["hello", "world"], + }, + }; + + convertedVal = toFormData(o); + expect(convertedVal instanceof FormData).toEqual(true); + expect(Array.from(convertedVal.keys()).length).toEqual(3); + expect(convertedVal.get("val")).toEqual("123") + expect(convertedVal.get("nested.arr.0")).toEqual("hello") + }); +}); From 2d139965ae108f634c36fe5af3310128d81799c1 Mon Sep 17 00:00:00 2001 From: Ben Carp Date: Sat, 17 Apr 2021 20:12:14 +0300 Subject: [PATCH 2/5] adding toFormData --- lib/helpers/toFormData.js | 55 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 lib/helpers/toFormData.js diff --git a/lib/helpers/toFormData.js b/lib/helpers/toFormData.js new file mode 100644 index 0000000000..e21d0a7fa6 --- /dev/null +++ b/lib/helpers/toFormData.js @@ -0,0 +1,55 @@ +'use strict'; + +function combinedKey(parentKey, elKey) { + return parentKey + '.' + elKey; +} + +function buildFormData(formData, data, parentKey) { + if (Array.isArray(data)) { + data.forEach(function buildArray(el, i) { + buildFormData(formData, el, combinedKey(parentKey, i)); + }); + } else if ( + typeof data === 'object' && + !(data instanceof File || data === null) + ) { + Object.keys(data).forEach(function buildObject(key) { + buildFormData( + formData, + data[key], + parentKey ? combinedKey(parentKey, key) : key + ); + }); + } else { + if (data === undefined) { + return; + } + + var value = + typeof data === 'boolean' || typeof data === 'number' + ? data.toString() + : data; + formData.append(parentKey, value); + } +} + +/** + * convert a data object to FormData + * + * type FormDataPrimitive = string | Blob | number | boolean + * interface FormDataNest { + * [x: string]: FormVal + * } + * + * type FormVal = FormDataNest | FormDataPrimitive + * + * @param {FormVal} data + */ + +module.exports = function getFormData(data) { + var formData = new FormData(); + + buildFormData(formData, data); + + return formData; +}; From 87ae9215e2269cdac6b70475a923f70a1b845aa8 Mon Sep 17 00:00:00 2001 From: carpben Date: Sat, 12 Feb 2022 18:33:07 +0200 Subject: [PATCH 3/5] initial multipart-FormData playground --- examples/postMultipartFormData/index.html | 131 ++++++++++++++++++++++ examples/postMultipartFormData/server.js | 10 ++ 2 files changed, 141 insertions(+) create mode 100644 examples/postMultipartFormData/index.html create mode 100644 examples/postMultipartFormData/server.js diff --git a/examples/postMultipartFormData/index.html b/examples/postMultipartFormData/index.html new file mode 100644 index 0000000000..1727a7c816 --- /dev/null +++ b/examples/postMultipartFormData/index.html @@ -0,0 +1,131 @@ + + + + axios - post example + + + + + +

Post multipart FormData

+ +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ +
+ +
+ + + + +
+
+ +
+ + + + + diff --git a/examples/postMultipartFormData/server.js b/examples/postMultipartFormData/server.js new file mode 100644 index 0000000000..4e0fafa867 --- /dev/null +++ b/examples/postMultipartFormData/server.js @@ -0,0 +1,10 @@ +'use strict'; +module.exports = function(req, res) { + req.on('end', function() { + res.writeHead(200, { + 'Content-Type': 'text/json' + }); + + res.end(); + }); +}; From cbb125f94e4af7dd2f7499165b5ee757a70984b9 Mon Sep 17 00:00:00 2001 From: carpben Date: Sat, 12 Feb 2022 20:00:10 +0200 Subject: [PATCH 4/5] formdata playground title --- examples/postMultipartFormData/index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/postMultipartFormData/index.html b/examples/postMultipartFormData/index.html index 1727a7c816..ac35060d0e 100644 --- a/examples/postMultipartFormData/index.html +++ b/examples/postMultipartFormData/index.html @@ -22,7 +22,7 @@ -

Post multipart FormData

+

Post multipart/form-data

From 9d5616edc8d4d743405f2bc30f355580fe4cbdd1 Mon Sep 17 00:00:00 2001 From: carpben Date: Sat, 12 Feb 2022 21:58:53 +0200 Subject: [PATCH 5/5] Fix examples/postMultipartFormData/server.js to the examples/**/server.js format, and so event end runs. --- examples/postMultipartFormData/server.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/examples/postMultipartFormData/server.js b/examples/postMultipartFormData/server.js index 4e0fafa867..b1be6b3bdc 100644 --- a/examples/postMultipartFormData/server.js +++ b/examples/postMultipartFormData/server.js @@ -1,10 +1,13 @@ -'use strict'; -module.exports = function(req, res) { - req.on('end', function() { +module.exports = function (req, res) { + + req.on('data', function (chunk) { + }); + + req.on('end', function () { + console.log('POST received'); res.writeHead(200, { 'Content-Type': 'text/json' }); - res.end(); }); };