Home > Net >  How to make API server calls in a Capacitor native app?
How to make API server calls in a Capacitor native app?

Time:11-29

How do you make API server calls using Axios in a Capacitor native app on Android or iOS (including on emulators)?

CodePudding user response:

In your main index.js file of your React app you need to globally define a baseUrl for axios so that all (relative) axios calls reach the server correctly:

axios.defaults.baseURL = 'https://www.example.com';

as per the documentation: https://axios-http.com/docs/config_defaults - otherwise Capacitor will point it to the localhost, which gave me problems.

CodePudding user response:

If you are running ionic angular you can use ionics HTTP (its a cordova plugin but it works well with capacitor) and make a http interceptor (must be the last interceptor if you have other interceptor and for web just use handleNext) if not i think you could still use the http community plugin available in capacitor

  • Related