Search code examples
javascriptmultipartform-dataform-data

How to nest form data


I want to have the following json example:

{
    attachments: {
        file: [
            { name: 'pic1.jpg' },
            { name: 'pic2.png' }
        ], 
        username: 'Test',
        age: 1
    }
}

is this possible in formdata?


Solution

  • OK, i figured it out. Just pass another key before array key... like:

    fd = new FormData();
    const files = [{...}, {...}, ...]
    files.map(file => {
        fd.append("attachments[] file[]", file.name)    
    });
    fd.append("attachments[] name", 'Test')
    fd.append("attachments[] age", 1)