0
0
Vueframework~10 mins

Axios interceptors in Vue - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Axios interceptors
Request made by Vue component
Request Interceptor runs
Request sent to server
Response received from server
Response Interceptor runs
Response delivered to Vue component
This flow shows how Axios interceptors catch requests before sending and responses after receiving, allowing modification or handling.
Execution Sample
Vue
import axios from 'axios';

axios.interceptors.request.use(config => {
  config.headers['X-Test'] = '123';
  return config;
});

axios.interceptors.response.use(response => {
  response.data.modified = true;
  return response;
});
This code adds headers before requests and modifies response data after receiving it.
Execution Table
StepActionInput/StateOutput/StateNotes
1Vue component calls axios.get('/api/data')No headersRequest config with URL '/api/data'Start request
2Request interceptor runsRequest configRequest config with header 'X-Test: 123'Header added
3Request sent to serverRequest config with headerWaiting for responseRequest sent
4Response receivedRaw response {data: {value: 42}}Raw responseServer replied
5Response interceptor runsRaw responseResponse with data.modified = trueResponse data modified
6Vue component receives responseModified responseData with value 42 and modified trueFinal data delivered
7EndN/AN/ARequest cycle complete
💡 Request and response cycle ends after interceptors modify and deliver data.
Variable Tracker
VariableStartAfter Step 2After Step 5Final
config.headers{}{"X-Test": "123"}{"X-Test": "123"}{"X-Test": "123"}
response.data{value: 42}{value: 42}{value: 42, modified: true}{value: 42, modified: true}
Key Moments - 2 Insights
Why does the request interceptor need to return the config object?
Because axios uses the returned config to send the request. If you don't return it (see Step 2 in execution_table), the request won't proceed.
What happens if the response interceptor does not return the response?
The Vue component will not receive the response data (see Step 5). Returning the response is necessary to continue the chain.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what header is added by the request interceptor at Step 2?
A'X-Test: 123'
B'Authorization: Bearer token'
C'Content-Type: application/json'
DNo header added
💡 Hint
Check the Output/State column at Step 2 in the execution_table.
At which step does the response data get modified?
AStep 3
BStep 5
CStep 1
DStep 6
💡 Hint
Look for 'Response data modified' note in the execution_table.
If the request interceptor did not return the config, what would happen?
ARequest would still send normally
BResponse interceptor would run twice
CRequest would be blocked and not sent
DVue component would receive an error immediately
💡 Hint
Refer to the key_moments explanation about returning config in Step 2.
Concept Snapshot
Axios interceptors let you run code before requests and after responses.
Use axios.interceptors.request.use(fn) to modify requests.
Use axios.interceptors.response.use(fn) to modify responses.
Always return config or response to continue the chain.
Useful for adding headers, logging, or error handling.
Full Transcript
Axios interceptors in Vue let you catch requests before they go out and responses when they come back. The request interceptor can add headers or change the request settings. The response interceptor can change the data before your component sees it. Both interceptors must return their input to keep the process going. This way, you can add things like authentication tokens or modify data globally in one place.