0
0
Vueframework~20 mins

Axios interceptors in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Axios Interceptor Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a request interceptor modifies headers?

Consider a Vue 3 component using Axios with a request interceptor that adds a custom header. What will be the value of the Authorization header sent with the request?

Vue
import axios from 'axios';
import { defineComponent } from 'vue';

const api = axios.create();

api.interceptors.request.use(config => {
  config.headers['Authorization'] = 'Bearer token123';
  return config;
});

export default defineComponent({
  async mounted() {
    const response = await api.get('/data');
    console.log(response.config.headers['Authorization']);
  }
});
A'Bearer token123'
Bnull
Cundefined
DThrows an error
Attempts:
2 left
💡 Hint

Think about what the interceptor does to the request config before sending.

state_output
intermediate
2:00remaining
What is the output after a response interceptor modifies data?

A response interceptor modifies the response data by adding a new property. What will be logged in the component?

Vue
import axios from 'axios';
import { defineComponent } from 'vue';

const api = axios.create();

api.interceptors.response.use(response => {
  response.data.modified = true;
  return response;
});

export default defineComponent({
  async mounted() {
    const response = await api.get('/info');
    console.log(response.data.modified);
  }
});
Afalse
Btrue
CThrows a TypeError
Dundefined
Attempts:
2 left
💡 Hint

Check what the interceptor does to the response.data object.

📝 Syntax
advanced
2:00remaining
Which option correctly adds an error interceptor in Axios?

Identify the correct syntax to add an error handler in an Axios response interceptor.

Aaxios.interceptors.response.use(response => response).catch(error => Promise.reject(error));
Baxios.interceptors.response.use(response => response, error => throw error);
Caxios.interceptors.response.use(response => response, error => Promise.reject(error));
Daxios.interceptors.response.catch(error => Promise.reject(error));
Attempts:
2 left
💡 Hint

Remember the use method takes two functions: one for success, one for error.

🔧 Debug
advanced
2:00remaining
Why does this Axios interceptor cause infinite requests?

Given the code below, why does the app keep sending requests endlessly?

Vue
import axios from 'axios';

const api = axios.create();

api.interceptors.response.use(response => {
  if (response.status === 401) {
    api.get('/refresh-token');
  }
  return response;
});
AThe interceptor is missing an error handler, causing unhandled exceptions.
BThe interceptor does not return the response, causing a hang.
CThe interceptor modifies the response status incorrectly, causing errors.
DThe interceptor triggers a new request inside itself without stopping, causing infinite loops.
Attempts:
2 left
💡 Hint

Think about what happens when the interceptor calls api.get inside itself.

🧠 Conceptual
expert
3:00remaining
How to skip interceptors for a specific Axios request?

You want to send a request using Axios but skip all interceptors for it. Which approach achieves this?

ACreate a new Axios instance without interceptors and use it for that request.
BSet a custom config property <code>skipInterceptors: true</code> in the request options.
CUse <code>axios.request</code> with <code>interceptors: false</code> option.
DRemove all interceptors before the request and add them back after.
Attempts:
2 left
💡 Hint

Interceptors are attached to Axios instances, so think about instance usage.