0
0
Vueframework~20 mins

Axios setup and configuration in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Axios Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when Axios is configured with a base URL?

Consider a Vue component that uses Axios with a base URL set. What will be the full request URL when making a GET request to '/users'?

Vue
import axios from 'axios';

const apiClient = axios.create({
  baseURL: 'https://api.example.com/v1'
});

export default {
  methods: {
    fetchUsers() {
      return apiClient.get('/users');
    }
  }
};
AThe request URL will be 'https://api.example.com/v1//users' with a double slash.
BThe request URL will be '/users' only, ignoring the base URL.
CThe request URL will be 'https://api.example.com/users', missing the '/v1' segment.
DThe request URL will be 'https://api.example.com/v1/users'.
Attempts:
2 left
💡 Hint

Think about how Axios combines the base URL with the request path.

📝 Syntax
intermediate
1:30remaining
Which Axios configuration option sets a timeout for requests?

Identify the correct Axios configuration option to set a request timeout of 5000 milliseconds.

A{ timeout: 5000 }
B{ time: 5000 }
C{ delay: 5000 }
D{ wait: 5000 }
Attempts:
2 left
💡 Hint

Look for the option that controls how long Axios waits before aborting a request.

🔧 Debug
advanced
2:30remaining
Why does this Axios request fail with a CORS error?

Examine the Axios request code below. Why might it cause a CORS error in the browser?

Vue
import axios from 'axios';

axios.get('https://api.example.com/data', {
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer token123'
  }
}).then(response => {
  console.log(response.data);
}).catch(error => {
  console.error(error);
});
AThe 'mode' option is not valid in Axios and does not affect CORS, causing the error.
BAxios automatically disables CORS, so the error is unrelated to this code.
CThe 'Content-Type' header must be 'text/plain' to avoid CORS errors.
DThe 'Authorization' header is missing, causing the server to reject the request.
Attempts:
2 left
💡 Hint

Check which options Axios supports and how CORS is handled in browsers.

state_output
advanced
2:00remaining
What is the value of 'responseData' after this Axios call?

Given the following Vue component method using Axios, what will be the value of this.responseData after the call?

Vue
import axios from 'axios';

export default {
  data() {
    return { responseData: null };
  },
  methods: {
    async loadData() {
      try {
        const response = await axios.get('https://api.example.com/items');
        this.responseData = response.data.items;
      } catch (error) {
        this.responseData = [];
      }
    }
  }
};
AThe entire Axios response object including headers and status.
BNull, because the assignment is asynchronous and not awaited.
CAn array of items from the server response, or an empty array if the request fails.
DUndefined, since 'responseData' is not initialized in data().
Attempts:
2 left
💡 Hint

Consider what response.data.items represents and how errors are handled.

🧠 Conceptual
expert
3:00remaining
Which Axios interceptor setup correctly logs request URLs before sending?

Choose the correct way to add a request interceptor in Axios that logs the request URL before the request is sent.

Aaxios.use.interceptors.request(config => { console.log(config.url); return config; });
Baxios.interceptors.request.use(config => { console.log(config.url); return config; });
Caxios.request.interceptors.use(config => { console.log(config.url); return config; });
Daxios.interceptors.use('request', config => { console.log(config.url); return config; });
Attempts:
2 left
💡 Hint

Recall the correct Axios interceptor syntax for requests.