axios post request with formData using AxiosInstance

27 Views Asked by At

I need to send a binary file through axios post request-

let data= await axios.post(url, formDataObj,config)

here formDataObj is object of FormData and config is an object that contains headers for API

This is working fine for me But I want to use axiosInstance.request() for this instead of axios.post()

const response = await axiosInstance.request(reqObj);

how can i pass formData in it? what is the syntax for that?

1

There are 1 best solutions below

1
Nijat Aliyev On BEST ANSWER

Example code maybe helpful

const formData = new FormData();
formData.append('someFile', myFile, 'test.jpg'); 
formData.append('anotherField', 'value');

const reqConfig = {
  method: 'post',
  url: 'your-api-url',
  data: formData,
  headers: {
    // Add headers here
  }
};

const response = await axiosInstance.request(reqConfig);