when using axios promise library, it allows you to set query defaults to be used on all requests like this:
axios.defaults.baseURL = 'localhost:3000'
axios.defaults.headers.common['Token'] = window.localStorage.authtoken || null
axios.defaults.headers.post['Content-Type'] = 'application/json'
This has been fine while there was only one API to query, but now I have multiple API's my Client application needs to interract with, is there a way to set multiple baseURL's with their own configurations? Or is there a way to tell axios to ignore the defaults on a particular request?
// on specific urls I want to override the default base URL
axios.get('localhost:9000/whatever_resource')
.then(result => {
// whatever
})
.catch(error => {
// whatever
})
You could set up an instance for each API, with their own base URLs: https://github.com/axios/axios#creating-an-instance
const firstAPI = axios.create({
baseURL: 'http://first-api.com'
})
const secondAPI = axios.create({
baseURL: 'http://second-api.com'
})
You can then use all the axios methods, like .get
and .post
on these instances:
firstAPI.get('hello')
.then((response) => {
console.log(response)
})
secondAPI.post('world')
.then((response) => {
console.log(response)
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With