📖 Scenario: You are building a Vue app that fetches user data from an API. You want to add a way to automatically add an authorization token to every request and handle errors globally.
🎯 Goal: Build a Vue component that uses Axios interceptors to add an authorization header to all requests and logs errors from responses.
📋 What You'll Learn
Create an Axios instance
Add a request interceptor to include an authorization token
Add a response interceptor to catch errors
Use the Axios instance in a Vue component to fetch data
💡 Why This Matters
🌍 Real World
Many web apps need to communicate with APIs securely and handle errors consistently. Axios interceptors help automate adding tokens and error handling.
💼 Career
Understanding Axios interceptors is essential for frontend developers working with Vue or other frameworks to build robust API integrations.
Progress0 / 4 steps
1
Create Axios instance
Create an Axios instance called apiClient using axios.create() with the base URL set to 'https://api.example.com'.
Vue
Hint
Use axios.create() and pass an object with baseURL.
2
Add request interceptor
Add a request interceptor to apiClient using apiClient.interceptors.request.use. The interceptor should add an Authorization header with the value 'Bearer mytoken123' to the config headers.
Vue
Hint
The request interceptor receives config. Add the header and return config.
3
Add response interceptor
Add a response interceptor to apiClient using apiClient.interceptors.response.use. The interceptor should return the response if successful, and if there is an error, log 'API error:' followed by the error to the console, then rethrow the error.
Vue
Hint
The response interceptor takes two functions: one for success, one for error. Log the error and rethrow it.
4
Use Axios instance in Vue component
Create a Vue component named UserList using <script setup>. Import ref and onMounted from 'vue' and apiClient. Create a users ref initialized to an empty array. Use onMounted to fetch /users from apiClient and assign the response data to users.value. Add a template that lists user names in an unordered list.
Vue
Hint
Use ref for reactive data and onMounted to fetch data once the component loads. Use v-for to list users.
Practice
(1/5)
1. What is the main purpose of Axios interceptors in a Vue application?
easy
A. To modify requests or responses globally before they reach your components
B. To create new Axios instances for each API call
C. To replace Vue lifecycle hooks with Axios methods
D. To automatically refresh the Vue component state
Solution
Step 1: Understand Axios interceptors role
Axios interceptors allow you to intercept and modify requests or responses globally before they reach your Vue components.
Step 2: Compare options
Options B, C, and D describe unrelated or incorrect uses of Axios interceptors.
Final Answer:
To modify requests or responses globally before they reach your components -> Option A
A. Missing return of config in the interceptor callback
B. Headers should be set on response, not config
C. Authorization header value must be a function
D. Interceptor should be added on axios.request, not axios.interceptors
Solution
Step 1: Check interceptor callback requirements
Axios interceptors must return the config object after modifying it to continue the request chain.
Step 2: Identify missing return
The code modifies headers but does not return config, causing the request to hang or fail.
Final Answer:
Missing return of config in the interceptor callback -> Option A
Quick Check:
Always return config in request interceptor [OK]
Hint: Always return config or response in interceptors [OK]
Common Mistakes:
Forgetting to return config or response
Setting headers on response instead of request config
Misplacing interceptor on wrong Axios property
5. You want to globally handle 401 Unauthorized errors in your Vue app using Axios interceptors. Which approach correctly implements this behavior?
hard
A. Use a response interceptor to retry the request infinitely on 401 errors
B. Use a request interceptor to modify the request URL on 401 errors
C. Use a request interceptor to add a 401 status header before sending
D. Use a response interceptor to check error response status and redirect to login
Solution
Step 1: Understand 401 error handling
401 Unauthorized errors come from server responses, so handling must be in a response interceptor.
Step 2: Choose correct handling method
Use a response interceptor to check error response status and redirect to login correctly uses a response interceptor to detect 401 status and redirect user to login page.
Step 3: Eliminate incorrect options
Retrying infinitely causes loops; using a request interceptor cannot handle response errors like 401; adding a 401 status header in the request is invalid.
Final Answer:
Use a response interceptor to check error response status and redirect to login -> Option D
Quick Check:
401 errors handled in response interceptor [OK]
Hint: Handle server errors in response interceptors, not request ones [OK]
Common Mistakes:
Trying to handle response errors in request interceptors
Retrying requests infinitely on 401 errors
Adding fake status headers in requests
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.