简体   繁体   中英

Axios request only work with react-native-debugger

I'm working on a tablet application where I need to fetch some data on the first page. When testing the route with Insomnia/Postman and when making that request by the app on a simulated device, with react-native-debugger on, everything works. But when I'm without the react-native-debugger or in a real device, the request doesn't work. Trying to see the request through a "proxy" I could see that the request returns an 403 error.

Where I create the HTTP Client instance:

export default axios.create({
  baseURL: 'https://api.lanchecard.com.br/api',
  headers: {
    Accept: 'application/json',
    Authorization: `Bearer ${token}`,
  },
});

And where I'm trying to consume data from an API:

useEffect(() => {
    http
      .get<IBranches[]>('/cantina')
      .then((response: any) => {
        console.log(response);
        setBranchesList(response.data);
      })
      .catch((error: any) => {
        console.log(error);
        setBranchesList([]);
      });
  }, []);

Already tested to see if the token was expirated or invalid, but it isn't, always using an valid token.

Making some kind of a "hack" I could make it work by specifing a User-Agent when creating the axios instance.

export default axios.create({
  baseURL: 'https://api.lanchecard.com.br/api',
  headers: {
    Accept: 'application/json',
    Authorization: `Bearer ${token}`,
    'User-Agent': 'Mozilla/5.0',
  },
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM