fetchival icon indicating copy to clipboard operation
fetchival copied to clipboard

Using FormData instead of json

Open harishsn opened this issue 8 years ago • 1 comments

Need support in posting data as FormData

harishsn avatar Jan 24 '17 14:01 harishsn

Found a function to convert object / array to FormData.

    var toFormData = function(obj, form, namespace) {

      var fd = form || new FormData();
      var formKey;

      for(var property in obj) {
        if(obj.hasOwnProperty(property)) {
          if(namespace) {
            formKey = namespace + '[' + property + ']';
          } else {
            formKey = property;
          }
          // if the property is an object, but not a File,
          // use recursivity.
          if(typeof obj[property] === 'object' && !(obj[property] instanceof File)) {
            toFormData(obj[property], fd, property);
          } else {
            // if it's a string or a File object
            fd.append(formKey, obj[property]);
          }
        }
      }
      return fd;
    };

Maybe there is a better solution...

pwFoo avatar Jul 08 '18 07:07 pwFoo