Why my image upload logic is always giving me error react native?

Viewed 242

I got a postman collection like this

postman headerHeader Image

postman bodyBody

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

1 Answers

in header try "Content-Type": "multipart/form-data" because you are sending an file not json object

Related