'Ionic 6 react with react-query fetching API using Axios does not work on real android device (production) but works on emulator and browser(dev)

I have a form that submit data to a server using react-hook-form like this:

<FormProvider {...methods}>
          <form onSubmit={handleSubmit(onIndividualSignup)}>
            <Swiper
              onSwiper={(swiper) => setSlidesRef(swiper)}
              speed={400}
              initialSlide={0}
              preventInteractionOnTransition={true}
            >
              <SwiperSlide>
                <FirstInfo next={nextSlide} />
              </SwiperSlide>
              <SwiperSlide>
                <SecondInfo prev={prevSlide} isLoading={isLoading} />
              </SwiperSlide>
            </Swiper>
          </form>
        </FormProvider>

Here I'm using the swiper/react package to create a multi-step form that works well.

Also using react-query to fetch data and Axios. To make my code reusable, I decided to create a hook for it.

export const useSignupMutation = () =>
  useMutation(
    async (user: IndividualForm) => {
      const response: AxiosResponse = await axios.post(
        `${API_URL}/api/v1/users/signup`,
        user,
        {
          headers: {
            "Content-Type": "application/json",
          },
        }
      );
      return response;
    },
    {
      onError(err) {
        console.log(err);
        Toast.show({
          text: String(err),
          duration: "long",
        });
      },
    }
  );

On Signing up for the form.

const onSignup: SubmitHandler<FormData> = (formData) => {
    console.log(formData);
    const { userType, email, fullName, username, password } = formData;

    mutate(
      { userType, email, fullName, username, password },
      {
        onSuccess: (data) => {
          queryClient.setQueryData("IndividualData", formData);
          const token: string = data.data?.token;
          setStorage("token", token);
          presentToast({
            message: "Please verify your email to continue",
            position: "bottom",
            color: "primary",
            duration: 4000,
            cssClass: "toast-container",
          });
          dismissToast();
          history.push("/page/Auth/VerifyEmail");
        },
        onError: (error) => {
          const err = error as AxiosError;
          presentToast({
            color: "danger",
            message: `${err.response?.data}`,
            position: "bottom",
            duration: 4000,
            cssClass: "toast-container",
          });
          dismissToast();
        },
      }
    );
  };

The API is consumed on Emulator and Browser but does not work on Real Android devices.

The Error comes below

Error on Real Android device

Also tried setting some configs on capacitor.config.ts

server: {
    androidScheme: "http",
    allowNavigation: ["https://my-api.com"],
    cleartext: true,
  },

Please we are stuck here for weeks... Thanks in advance.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source