Home > Software design >  React Native Axios Network Error but works fine on postman client
React Native Axios Network Error but works fine on postman client

Time:11-01

I'm trying to get axios working on a react native project to reach a backend but I am getting the Network Error issue that I just can't seem to debug.

 const searchApi = async () => {
  try {
    let res = await axios.get('https://product.company.com/api/documents/invoices');
    console.log(res);
  } catch (err) {
    console.log(err);
  }
 }

So if I was to do a get request to the example url provided via Thunder Client or Postman Client, I get the appropriate response of

{
    "status": "401 error",
    "message": "Token not found."
}

But when done through axios, I seem to get a network error. I'm a little unsure how I can debug this too to get more error logs.

CodePudding user response:

Try with below code:

Replace with Your URL

    var config = {
      method: 'get',
      url: 'https://reqres.in/api/users?page=1',
      headers: { 
        'Content-Type': 'application/json'
      }
    };
    
    axios(config).then(function (response) {
      console.log(JSON.stringify(response.data));
    })
    .catch(function (error) {
      console.log(error);
    });

CodePudding user response:

It's not a network error but an unauthorized error. You need to pass some authenticated token in the header.

  • Related