error: "invalid response for blob" in android, using sdk 28.0.0

#1

I am getting this error as a network request is sent using fetch while the device is offline.
It is working fine if device has a working internet connection but as I disable the internet, Instead of getting in catch, the app throws this error. I am using an android device.

This is my code snippet:

export const sendOTP = (phone, successCallback, failedCallback) => dispatch => {
    fetch(url, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify({
            phone: '91'+phone,
        })
    })
    .then((response) => response.json())
    .then((res) => {
        if(res.success) {
            dispatch({type: ADD_PHONE_NUMBER, payload: { phone:'91'+ phone }});
            successCallback();
        }
    })
    .catch((err) => {
        failedCallback();
    })
}

I think it’s an issue in React Native 0.55

Can anyone guide me how to fix this?

#2

Hey @priyanshrastogi how is it going? Thanks for using Expo :slight_smile:

It seems to me in the worst case you could just wrap the entire function call in a try/catch, if you don’t want to do that, you could debug each callback method.

Also with async/await you could write this method like:

async () => {
   const response = await fetch(...);
   const data = await response.json();
   
   if (!data.success) {
      console.log('error case');
      return;
   }
   
   console.log('success case');
   return;
} 

Which might make it easier to debug

1 Like
#3

Thank you for replying. I was still getting the same error. I think this is an issue with RN 0.55 #18840

But then I used axios instead of fetch and it’s working fine.

#5

@priyanshrastogi When you switched to axios, did you do anything else besides converting fetch(url).then(response => response.json()) to axios.get(url)? I switched over to axios and it rejects every request I make.

closed #6

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.