Change the default base url for axios

Geoff picture Geoff · Nov 21, 2017 · Viewed 87k times · Source

I have configured my axios like this

const axiosConfig = {
  baseURL: 'http://127.0.0.1:8000/api',
  timeout: 30000,
};

Vue.prototype.$axios = axios.create(axiosConfig)

Inside my component i make a call as

this.$axios.get('items').then()..

Now the above works but i would like to change the baseURL without affecting the global base URL so that in my component i can simply use without API endpoint so

I've tried

this.$axios.baseURL = "http://127.0.0.1:8000";
this.$axios.get().. //this is still in api endpoint

How do i go about this?

Answer

Vipin Kumar picture Vipin Kumar · Nov 21, 2017

Instead of

this.$axios.get('items')

use

this.$axios({ url: 'items', baseURL: 'http://new-url.com' })

If you don't pass method: 'XXX' then by default, it will send via get method.

Request Config: https://github.com/axios/axios#request-config