therealparmesh / object-to-formdata

Convenient JavaScript function that serializes Objects to FormData instances.
MIT License
370 stars 62 forks source link

Does this library convert any cases of nested objects and arrays? #253

Open saver711 opened 1 year ago

saver711 commented 1 year ago

Maybe something like this:

const obj = {
  name: "ahmed",
  z: {
    fName: "mo",
    lName: "xx",
    arr: [{ a: 1 }, { b: 2 }, {file: FileObj}, data: {name:'zzzz'}]
  }
};
ericus20 commented 1 year ago

It doesn't look like it. I'm currently facing the same issue

VictorPulzz commented 1 year ago

@saver711 I checked how this lib work with nested objects and array, and it seems to be doing well:

serialize(
  {
    asd: 1,
    b: [
        {
         a: 2,
         c: {
                   f: 3,
             o: [
                      {
                          s: 1,
                      },
                  ],
               },
           },
       ],
       name: 'ahmed',
       z: {
              fName: 'mo',
              lName: 'xx',
              arr: [{ a: 1 }, { b: 2 }, { file: body['user:picture'] }],
          },
      },
      {
         indices: true,
      },
)

Picture from log react-native: image Picture from log proxyman: --q7Hh_97O8Zean.puWe0UDus5sRtLnPKCrxKPdTgAmgZnfoJhQFoaZbTrvolJ_OAZzs4HOH content-disposition: form-data; name="asd"

1 --q7Hh_97O8Zean.puWe0UDus5sRtLnPKCrxKPdTgAmgZnfoJhQFoaZbTrvolJ_OAZzs4HOH content-disposition: form-data; name="b[0][a]"

2 --q7Hh_97O8Zean.puWe0UDus5sRtLnPKCrxKPdTgAmgZnfoJhQFoaZbTrvolJ_OAZzs4HOH content-disposition: form-data; name="b[0][c][f]"

3 --q7Hh_97O8Zean.puWe0UDus5sRtLnPKCrxKPdTgAmgZnfoJhQFoaZbTrvolJ_OAZzs4HOH content-disposition: form-data; name="b[0][c][o][0][s]"

1 --q7Hh_97O8Zean.puWe0UDus5sRtLnPKCrxKPdTgAmgZnfoJhQFoaZbTrvolJ_OAZzs4HOH content-disposition: form-data; name="name"

ahmed --q7Hh_97O8Zean.puWe0UDus5sRtLnPKCrxKPdTgAmgZnfoJhQFoaZbTrvolJ_OAZzs4HOH content-disposition: form-data; name="z[fName]"

mo --q7Hh_97O8Zean.puWe0UDus5sRtLnPKCrxKPdTgAmgZnfoJhQFoaZbTrvolJ_OAZzs4HOH content-disposition: form-data; name="z[lName]"

xx --q7Hh_97O8Zean.puWe0UDus5sRtLnPKCrxKPdTgAmgZnfoJhQFoaZbTrvolJ_OAZzs4HOH content-disposition: form-data; name="z[arr][0][a]"

1 --q7Hh_97O8Zean.puWe0UDus5sRtLnPKCrxKPdTgAmgZnfoJhQFoaZbTrvolJ_OAZzs4HOH content-disposition: form-data; name="z[arr][1][b]"

2 --q7Hh_97O8Zean.puWe0UDus5sRtLnPKCrxKPdTgAmgZnfoJhQFoaZbTrvolJ_OAZzs4HOH content-disposition: form-data; name="z[arr][2][file]"; filename="IMG_0005.JPG" content-type: image/jpeg

�����JFIF��H�H����&�Exif��MM�*���� ������������ ���������������������������(�������1��������2��������;����������������������� �i�������%������b��<NIKON CORPORATION�NIKON D800E����H������H���Aperture ....