Reputation: 454
I got a postman collection like this
Here is my upload logic. I am using react-native-image-picker
to access the photo library
Form making logic
const createFormData = (photo, body = {}) => {
const data = new FormData();
data.append('files', {
files: photo.assets[0].fileName,
type: photo.assets[0].type,
uri:
Platform.OS === 'ios'
? photo.assets[0].uri.replace('file://', '')
: photo.assets[0].uri,
});
return data;
};
upload logic
const handleUploadPhoto = () => {
createFormData(photo);
axios
.post(
'userapi/UpdateUserProfileImage',
{
body: createFormData(photo),
},
{
headers: {
'Content-Type': 'application/json',
'Authorization': '...'
},
},
)
.then(response => {
console.log('response', response.data);
})
.catch(error => {
console.log('error', error);
});
};
I am always getting 500 error
Upvotes: 2
Views: 493
Reputation: 26
in header try "Content-Type": "multipart/form-data" because you are sending an file not json object
Upvotes: 1