import { endpoint } from 'app/api';
import asyncActionCreator from './asyncActionCreator';

export const ingestDocument = asyncActionCreator(
  (collectionId, metadata, file, onUploadProgress, cancelToken) => async () => {
    const formData = new FormData();
    if (file) {
      formData.append('file', file);
    }
    formData.append('meta', JSON.stringify(metadata));
    const config = {
      onUploadProgress,
      headers: {
        'content-type': 'multipart/form-data',
      },
      params: { sync: true },
      cancelToken,
    };
    const response = await endpoint.post(
      `collections/${collectionId}/ingest`,
      formData,
      config
    );
    return { ...response.data };
  },
  { name: 'INGEST_DOCUMENT' }
);

import { endpoint } from 'app/api';
import asyncActionCreator from './asyncActionCreator';

export const ingestDocument = asyncActionCreator(
  (collectionId, metadata, file, onUploadProgress, cancelToken) => async () => {
    const formData = {
      meta: JSON.stringify(metadata),
      file: file,
    };

    const config = {
      onUploadProgress,
      params: { sync: true },
      cancelToken,
    };

    const response = await endpoint.postForm(
      `collections/${collectionId}/ingest`,
      formData,
      config
    );

    return { ...response.data };
  },
  { name: 'INGEST_DOCUMENT' }
);

Use automatic form data serialization
Use automatic form data serialization

Starting from 0.27.0, axios supports automatic serialization to the form data format for requests with a `multipart/form-data` content type.
