0

'react-native-image-picker' for uploading image in my application, Sometimes it is uploading and sometimes i am getting [TypeError: Network request failed] below is the code:

FormData in my component:

//image is :file:///data/user/0/com.testApp/cache/rn_image_picker_lib_temp_0d38d959-6ece-4750-a215-4b3f68002f4e.jpg
let formData = new FormData();
  formData.append('images', { uri:image, name: imageSelected?.fileName, type: 'image/png' });
const response =  await updateUserProfile(userDetails,formData)

Service call:

 export const updateUserProfile = async (userDetails,data) => {
    const response = await fetch(`${baseUrl}/updateusersprofile/${userDetails._id}`, {
        method: "PATCH",
        headers: {
          //"Content-Type": "application/json",
          'Content-Type': 'multipart/form-data',
          Authorization: `Bearer ${userDetails.token}`,
        },
        body: data,
      });
    return await response;
  };

In Postman i have checked the api is working fine, What would be the problem in my code.

1 Answers1

0
 export const updateUserProfile = async (userDetails,data) => {
    const response = await fetch(`${baseUrl}/updateusersprofile/${userDetails._id}`, {
        method: "PATCH",
        headers: {
          //"Content-Type": "application/json",
          'Content-Type': 'multipart/form-data',
          Authorization: `Bearer ${userDetails.token}`,
        },
        body: JSON.stringify(data),
      });
    return await response;
  };
moken
  • 3,227
  • 8
  • 13
  • 23
  • Answer needs supporting information Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center https://stackoverflow.com/help/how-to-answer. – moken Jan 23 '23 at 10:34