I was previously using ApiSauce
to post listings from React Native
App to Node.js
with Multer
, and now I have migrated to Axios
everything went fine except for the uploading images part.
export const add = (listing, onUploadProgress) => {
const data = new FormData();
data.append('title', listing.title);
data.append('price', listing.price);
data.append('categoryId', listing.category.value);
data.append('description', listing.description);
data.append('userId', listing.userId);
listing.images.forEach((image, index) =>
data.append('images', {
name: 'image' + index,
type: 'image/jpeg',
uri: image,
}),
);
if (listing.location)
data.append('location', JSON.stringify(listing.location));
return client.post(endpoint, data, {
onUploadProgress: (progress) =>
onUploadProgress(progress.loaded / progress.total),
});
};