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.

2
  • You sure the type you've passed is correct? Instead of passing a static type:'image/png', pass the type retrieved from the image picker. Commented Jan 20, 2023 at 7:30
  • @TayyabMazhar It is working fine now after changing type to dynamic, still it is failing sometimes Commented Jan 20, 2023 at 10:35

1 Answer 1

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;
  };
1
  • 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 stackoverflow.com/help/how-to-answer.
    – moken
    Commented Jan 23, 2023 at 10:34

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.