Timeout expects a number representing milliseconds, but here it is a string '5000'.
Step 2: Validate other properties
baseURL is correctly placed, and headers with Content-Type is valid for axios.create().
Final Answer:
timeout should be a number, not a string -> Option A
Quick Check:
timeout must be number, not string [OK]
Hint: Timeout must be numeric, not quoted string [OK]
Common Mistakes:
Putting baseURL inside headers
Removing headers property
Using string for timeout value
5. You want to create an Axios instance in Vue that automatically adds an Authorization header with a token stored in localStorage for every request. Which setup is correct?
hard
A. const api = axios.create({ baseURL: 'https://api.example.com' }); api.defaults.headers.common['Authorization'] = `Bearer ${localStorage.getItem('token')}`;
B. const api = axios.create({ baseURL: 'https://api.example.com' }); api.interceptors.request.use(config => { config.headers.Authorization = `Bearer ${localStorage.getItem('token')}`; return config; });
C. const api = axios.create({ baseURL: 'https://api.example.com', headers: { Authorization: `Bearer ${localStorage.getItem('token')}` } });
D. const api = axios.create({ baseURL: 'https://api.example.com', headers: { Authorization: localStorage.token } });
Solution
Step 1: Understand token dynamic retrieval
The token in localStorage can change, so it should be read fresh before each request, not just once at setup.
Step 2: Use Axios interceptor for dynamic headers
Using an interceptor lets you add or update headers before every request, ensuring the latest token is used.
Step 3: Evaluate options
const api = axios.create({ baseURL: 'https://api.example.com' }); api.interceptors.request.use(config => { config.headers.Authorization = `Bearer ${localStorage.getItem('token')}`; return config; }); uses an interceptor to set Authorization header dynamically. Options A and C set it once at creation, which may become stale. const api = axios.create({ baseURL: 'https://api.example.com', headers: { Authorization: localStorage.token } }); uses incorrect localStorage access.
Final Answer:
Use an interceptor to add Authorization header dynamically before each request -> Option B