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?
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']); } });
Think about what the interceptor does to the request config before sending.
The request interceptor adds the Authorization header to the config object before the request is sent. Therefore, the header will be 'Bearer token123' when logged.
A response interceptor modifies the response data by adding a new property. What will be logged in the component?
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); } });
Check what the interceptor does to the response.data object.
The response interceptor adds a modified property with value true to the response.data object. So logging response.data.modified outputs true.
Identify the correct syntax to add an error handler in an Axios response interceptor.
Remember the use method takes two functions: one for success, one for error.
Option C correctly uses use with two functions: one for handling the response, and one for handling errors by rejecting the promise.
Option C incorrectly chains catch on use, which is not valid.
Option C uses throw inside an arrow function without braces, causing syntax error.
Option C tries to call catch on interceptors.response, which is invalid.
Given the code below, why does the app keep sending requests endlessly?
import axios from 'axios'; const api = axios.create(); api.interceptors.response.use(response => { if (response.status === 401) { api.get('/refresh-token'); } return response; });
Think about what happens when the interceptor calls api.get inside itself.
The interceptor calls api.get inside the response interceptor. This triggers the interceptor again for the new request, causing an infinite loop of requests.
You want to send a request using Axios but skip all interceptors for it. Which approach achieves this?
Interceptors are attached to Axios instances, so think about instance usage.
Interceptors are bound to Axios instances. Creating a new instance without interceptors and using it for specific requests avoids running interceptors.
Options B and C use non-existent config options. Option A is risky and can cause race conditions.