1

I test it successfully by using postman. I get status code 200.

enter image description here

Using Axios in React Native 0.60.5

But when I test it on react native project, I get Error: Request failed with status code 415

I have no idea why.

Here is my axios code:

ApiServices.ts

import axios from 'axios';

const instance = axios.create({
  baseURL: 'http://192.168.1.161:5000/',
  timeout: 3000,
});

instance.interceptors.request.use((config) => {
  return config;
}, (error) => {
  // handle error
  return Promise.reject(error);
});

instance.interceptors.response.use((response) => {
  return response;
}, error => {
  // handle error
  return Promise.reject(error);
});


export const Get = async (api: string, params: object) => {
  console.log('api', api); // Account
  console.log('params', params); // { userAccount: "0989257556", userPwd: "12345" }
  return new Promise((resolve, reject) => {
    instance.get(api, params).then(res => {
      resolve(res.data);
    }).catch(error => {
      reject(error);
    });
  })
};

I use the function like this:

export const testApiGet = () => {

  const userAccount = '0989257556';
  const userPwd = '12345';

  Get('Account', { userAccount, userPwd }).then(res => {
    console.log('res ->', res);
  }).catch(err => {
    console.log('err ->', err);
  });
}

Any help would be appreciated.

Morton
  • 5,380
  • 18
  • 63
  • 118

1 Answers1

1

I was stuck at the same problem today for few hours. As far as I understood basicly you cant send GET request with body through axious. Just change the GET method to POST in your server. You can check this topick where I post more detailed answer about this.

Ili
  • 633
  • 9
  • 12