Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use 2 instances of Axios with different baseURL in the same app (vue.js)

I'm trying to learn vue.js so I made a little app that displays news articles from an API and, in another view, allows the user to log into another server.

For this I'm using Axios. I know I got it to work pretty well at some point, but today when starting my project, it's just impossible to get both apis to work simultaneously.

Here is my login service:

import axiosTrainingAPI from 'axios'  axiosTrainingAPI.defaults.baseURL = 'https://api.**********.com'  const trainingAPI = {   login (credentials) {     return new Promise((resolve, reject) => {       axiosTrainingAPI.post('/services/auth.php', credentials)         .then(response => {           resolve(response.data)         }).catch(response => {           reject(response.status)         })     })   } }  export default trainingAPI 

Here is my news service:

import axiosGoogleNewsAPI from 'axios'  axiosGoogleNewsAPI.defaults.baseURL = 'https://newsapi.org'  const googleNewsAPI = {   getPosts (newsId) {     return new Promise((resolve, reject) => {       axiosGoogleNewsAPI.get(`/v2/everything?q=${newsId}&sortBy=publishedAt&apiKey=***********`)         .then(response => {           resolve(response.data)         }).catch(response => {           reject(response.status)         })     })   } }  export default googleNewsAPI 

Both those services are in different JS files and are imported in different vue files but it seems that now they cannot coexist and there is always one overwriting the baseURL of the other (not always the same) almost like if the Axios instance was the same in both cases. So some time the first service uses the second one's baseURL, sometimes it's the second that uses the first one's baseURL...

I don't know exactly the scope of 'import' because it's pretty new to me but both instances are in different files, have different names so I don't really understand how they get mixed up. Except if 'import' always calls the same instance of a module but then how do I work with 2 apis? And why did it work yesterday... I'm confused.

like image 562
manu Avatar asked Nov 24 '17 17:11

manu


People also ask

How do I create a new instance of Axios with a custom config?

First, we require Axios as we have already installed it in the previous step. Then, we use axios. create to create a new instance of Axios with a custom config that has a base URL of https://api.GitHub.com/ and a timeout of 1s. The config also has an Accept header with value application/vnd.

What function can you call to create a new instance of Axios with a custom configuration?

In this example, we are using axios. create() to create a new instance of Axios with a custom configuration that has a base URL of http://localhost:3002/products and a timeout of 1000 milliseconds. The configuration also has an Accept and Authorization headers set depending on the API being invoked.

What is Axios defaults baseURL?

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

How does Axios work in Vue?

Axios is an Excellent HTTP library that executes on both client and a server, which makes an API request, does the task to produce the result and specifies easier concepts to read and debug. Vue. js is the front-end JavaScript Framework to build tools and libraries. The Axios works well in the platform like node.


1 Answers

You'll want to create a new instance of axios with a custom config for each API you want that has a distinct baseURL.

var instance = axios.create({   baseURL: 'https://some-domain.com/api/',   timeout: 1000,   headers: {'X-Custom-Header': 'foobar'} }); 
like image 57
PatrickSteele Avatar answered Sep 18 '22 13:09

PatrickSteele