React-native-fetch-blob GET request error

1k Views Asked by At

I am replacing axios to rn-fetch-blob in my react-native project. In the request I ping my server with credentials and I expect a response.

The old request with axios is as follows and works perfectly:

export const postWorkspace =
  (newWorkspace: Workspace): AppThunk =>
  async (dispatch) => {
    console.log('addWorkspace Start');

    dispatch(setIsLoading(true));
    let configOption = {
      headers: {
        'Access-Control-Allow-Origin': '*',
        'X-AUTH-USER': newWorkspace.credentials.email,
        'X-AUTH-TOKEN': newWorkspace.credentials.password,
      },
    };

    await axios
      .get(`${newWorkspace.url}/api/ping`, configOption)
      .then(async (resp) => {
        console.log('addWorkspace resp', resp);
        try {
          await storeWorkspaceToStorage(newWorkspace);
        } catch (e) {
          console.error(e);
        }
      })
      .catch((err) => {
        console.log('addWorkspace err', JSON.stringify(err));
        return Promise.reject(err);
      })
      .finally(() => dispatch(setIsLoading(false)));
  };

This is how I transformed the code with rn-fetch-blob:

export const postWorkspace= 
  (newWorkspace: Workspace): AppThunk => 
   async (dispatch) => {
     console.log('addWorkspace Start');

     dispatch(setIsLoading(true));
     let configOption = {
         'Access-Control-Allow-Origin': '*',
         'X-AUTH-USER': newWorkspace.credentials.email,
         'X-AUTH-TOKEN': newWorkspace.credentials.password,
     };

     await RNFetchBlob
     .fetch('GET', '${newWorkspace.url}/api/ping', configOption)

    .then( async(resp) => {
      console.log('addWorkspace resp', resp);
         try {
           await storeWorkspaceToStorage(newWorkspace);
         } catch (e) {
           console.error(e);
         }

    })
    
    .catch((err) => {
         //console.log(err.info().status); 
         console.log('addWorkspace err', JSON.stringify(err));
         return Promise.reject(err);
       })
    .finally(() => dispatch(setIsLoading(false)));
   };

The new request with rn-fetch-blob returns this error:

response error "line":126349,"column":34,"sourceURL":"http://localhost:8081/index.bundle?platform=android&dev=true&minify=false"

When I opend the file "http://localhost:8081/index.bundle?platform=android&dev=true&minify=false" around line 1262349 the code looks like this, I can't understand what went wrong:

var req = RNFetchBlob[nativeMethodName];
      req(options, taskId, method, url, headers || {}, body, function (err, rawType, data) {
        subscription.remove();
        subscriptionUpload.remove();
        stateEvent.remove();
        partEvent.remove();
        delete promise['progress'];
        delete promise['uploadProgress'];
        delete promise['stateChange'];
        delete promise['part'];
        delete promise['cancel'];

        promise.cancel = function () {};
        //line 126349
        if (err) reject(new Error(err, respInfo));else {
          if (options.path || options.fileCache || options.addAndroidDownloads || options.key || options.auto && respInfo.respType === 'blob') {
            if (options.session) session(options.session).add(data);
          }

          respInfo.rnfbEncode = rawType;
          resolve(new FetchBlobResponse(taskId, respInfo, data));
        }
      });
    });

I am doing this since rn-fetch-blob is basically one of the few libraries that allows react-native to ping a server with no SSL certification.

Thank you

0

There are 0 best solutions below