I am trying to upload multiple images to server using axio with multiple params as request body. Basically below is the request in native android which am trying to do it in react native.
@Headers(HTTPService.NEEDS_AUTH_HEADER)
@Multipart
@POST("/api/post")
Call<Post> create(
@Part("title") RequestBody title,
@Part("content") RequestBody content,
@Part("location") RequestBody location,
@Part("category") RequestBody category,
@Part("captions") List<RequestBody> captions,
@Part List<MultipartBody.Part> parts
);
and this is how am trying to do in react :
const config = {
method: 'post',
headers: {
'NEEDS_AUTH': true,
Accept: 'application/json',
'Content-type': undefined
},
formDataArray,
url: PATH_API_CREATE_POST,
data: {
title: postTitle,
content: postContent,
location: locationId,
category: categoryId,
}
}
axios(config).then(res => console.log('create post ', res)).catch(err => console.log('create post err', err.response))
Here it's formDataArray :
photos.forEach(element => {
let formdata = new FormData();
formdata.append("upload", { uri: element, name: 'image.jpg', type: 'image/jpg' })
formDataArray.push(formdata)
});
Unfortunately no images are uploaded to server.
'Content-type': undefinedto'Content-Type': 'multipart/form-data'."content-type missing boundaryformDataArray? Let me see.photos.forEach(element => { formdata.append("parts[]", { uri: element, name: 'image.jpg', type: 'image/jpg' }) }); formdata.append('title', postTitle); formdata.append('content', postContent); formdata.append('location', locationId); formdata.append('category', categoryId);and then do axios post withformdata, not formDataArray anymore.