Skip to content

Instantly share code, notes, and snippets.

@sam-ngu
Last active April 14, 2021 00:16
Show Gist options
  • Save sam-ngu/cf7acab50c9a3b54aa75d2d2d7c411e8 to your computer and use it in GitHub Desktop.
Save sam-ngu/cf7acab50c9a3b54aa75d2d2d7c411e8 to your computer and use it in GitHub Desktop.

Revisions

  1. sam-ngu revised this gist Apr 8, 2021. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion formdata.js
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,8 @@ for (let key in payload) {

    if (Array.isArray(value)) {
    value.forEach((item, index) => {
    // pack array content in FormData
    // pack array content in FormData using the append method
    // first argument is the key, and the second argument is the value
    // the form data key should look something like: 'recipients[0]', 'recipients[1]'
    formData.append(`${key}[${index}]`, item);
    });
  2. sam-ngu revised this gist Apr 8, 2021. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion formdata.js
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,11 @@
    // payload to send in the API request
    const payload = {
    email: "[email protected]",
    recipients: [
    '[email protected]',
    '[email protected]',
    ],
    attachment: fileObject,

    }

    // creating a new FormData instance
    @@ -17,6 +17,8 @@ for (let key in payload) {

    if (Array.isArray(value)) {
    value.forEach((item, index) => {
    // pack array content in FormData
    // the form data key should look something like: 'recipients[0]', 'recipients[1]'
    formData.append(`${key}[${index}]`, item);
    });
    } else {
  3. sam-ngu revised this gist Apr 8, 2021. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions formdata.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,9 @@
    const payload = {
    email: "[email protected]",

    recipients: [
    '[email protected]',
    '[email protected]',
    ],
    attachment: fileObject,

    }
    @@ -13,9 +16,8 @@ for (let key in payload) {
    let value = payload[key];

    if (Array.isArray(value)) {
    const valueKey = key;
    value.forEach((item, index) => {
    formData.append(`${valueKey}[${index}]`, item);
    formData.append(`${key}[${index}]`, item);
    });
    } else {
    formData.append(key, value);
  4. sam-ngu created this gist Feb 23, 2021.
    23 changes: 23 additions & 0 deletions formdata.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    const payload = {
    email: "[email protected]",

    attachment: fileObject,

    }

    // creating a new FormData instance
    const formData = new FormData();

    for (let key in payload) {

    let value = payload[key];

    if (Array.isArray(value)) {
    const valueKey = key;
    value.forEach((item, index) => {
    formData.append(`${valueKey}[${index}]`, item);
    });
    } else {
    formData.append(key, value);
    }
    }