Kerry
Kerry

Reputation: 454

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

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

Upvotes: 2

Views: 493

Answers (1)

Ahmed Khalil
Ahmed Khalil

Reputation: 26

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

Upvotes: 1

Related Questions