skip to Main Content

This has me stumped. I am working on the following:

  const testFetch = () => {
    console.log("testFetch");
    fetch(`${baseurl}/${requrl}`, {
      method: "POST",
      body: JSON.stringify({ foo: "bar" }),
      headers: {
        "Content-Type": "application/json; charset=utf-8",
        Accept: "*/*",
        "Cache-Control": "no-cache",
      },
    })
      .then((response) => {
        console.log("response", response);
      })
      .catch((err) => {
        console.log("error", err);
      });
  };

The server will return a 401 Unauthorized for this call. The promise never resolves in iOS (it does in Android). I.e. I never get to the response or error logging. Here it gets weird IMO:

  • When I remove the body payload the promise resolves
  • When I change the url to a non existent path the promise resolves (with a 404 not found)

I’m on Expo 15.0.7 and React Native 0.74.2. I’ve tried Axios, Apisauce and XMLHttpRequest to no avail. With postman I get a perfect response. It’s not cors related. This happens on Expo Go in the iOS simulator and on an iOS device. An Android device had no issue with this code. Having spent more than 24 hours on debugging this I turn the you. Thanks for your time.

2

Answers


  1. Chosen as BEST ANSWER

    I found the cause of this error. The server (IIS) had Basic and Digest auth on and included a www-authenticate header in the response because the code was returning a 401. What is important to realize, is that if this happens fetch/axios/apisauce/etc will hang in iOS and not resolve. I can only guess why. Probably because it is intercepted before it gets to the React Native code and some other logic tries to handle the authentication.


  2. I’m encountering the same issue where the POST API works in Android Expo but not in iOS Expo. However, when I try the same API in React Native CLI, it works. I’m stuck and don’t know what is happening.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search