Repository: bradtraversy/axios-crash
Branch: master
Commit: d664ffbd6bcf
Files: 4
Total size: 9.3 KB
Directory structure:
gitextract_klvj9h2b/
├── README.md
├── index.html
├── main.js
└── start.js
================================================
FILE CONTENTS
================================================
================================================
FILE: README.md
================================================
# Axios Crash Course
> These are the files for the YouTube Axios crash course.
"start.js" is just the event listeners and empty functions. "main.js" is the completed code
================================================
FILE: index.html
================================================
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<link
rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"
crossorigin="anonymous"
/>
<title>Axios Crash Course</title>
</head>
<body>
<div class="container my-5">
<div class="text-center">
<h1 class="display-4 text-center mb-3">Axios Crash Course</h1>
<button class="btn btn-primary my-3" id="get">GET</button>
<button class="btn btn-info" id="post">POST</button>
<button class="btn btn-warning" id="update">PUT/PATCH</button>
<button class="btn btn-danger" id="delete">DELETE</button>
<button class="btn btn-secondary" id="sim">Sim Requests</button>
<button class="btn btn-secondary" id="headers">
Custom Headers
</button>
<button class="btn btn-secondary" id="transform">
Transform
</button>
<button class="btn btn-secondary" id="error">
Error Handling
</button>
<button class="btn btn-secondary" id="cancel">
Cancel
</button>
</div>
<hr />
<div id="res"></div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.0/axios.min.js"></script>
<script src="main.js"></script>
</body>
</html>
================================================
FILE: main.js
================================================
// AXIOS GLOBALS
axios.defaults.headers.common['X-Auth-Token'] =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c';
// GET REQUEST
function getTodos() {
// axios({
// method: 'get',
// url: 'https://jsonplaceholder.typicode.com/todos',
// params: {
// _limit: 5
// }
// })
// .then(res => showOutput(res))
// .catch(err => console.error(err));
axios
.get('https://jsonplaceholder.typicode.com/todos?_limit=5', {
timeout: 5000
})
.then(res => showOutput(res))
.catch(err => console.error(err));
}
// POST REQUEST
function addTodo() {
axios
.post('https://jsonplaceholder.typicode.com/todos', {
title: 'New Todo',
completed: false
})
.then(res => showOutput(res))
.catch(err => console.error(err));
}
// PUT/PATCH REQUEST
function updateTodo() {
axios
.patch('https://jsonplaceholder.typicode.com/todos/1', {
title: 'Updated Todo',
completed: true
})
.then(res => showOutput(res))
.catch(err => console.error(err));
}
// DELETE REQUEST
function removeTodo() {
axios
.delete('https://jsonplaceholder.typicode.com/todos/1')
.then(res => showOutput(res))
.catch(err => console.error(err));
}
// SIMULTANEOUS DATA
function getData() {
axios
.all([
axios.get('https://jsonplaceholder.typicode.com/todos?_limit=5'),
axios.get('https://jsonplaceholder.typicode.com/posts?_limit=5')
])
.then(axios.spread((todos, posts) => showOutput(posts)))
.catch(err => console.error(err));
}
// CUSTOM HEADERS
function customHeaders() {
const config = {
headers: {
'Content-Type': 'application/json',
Authorization: 'sometoken'
}
};
axios
.post(
'https://jsonplaceholder.typicode.com/todos',
{
title: 'New Todo',
completed: false
},
config
)
.then(res => showOutput(res))
.catch(err => console.error(err));
}
// TRANSFORMING REQUESTS & RESPONSES
function transformResponse() {
const options = {
method: 'post',
url: 'https://jsonplaceholder.typicode.com/todos',
data: {
title: 'Hello World'
},
transformResponse: axios.defaults.transformResponse.concat(data => {
data.title = data.title.toUpperCase();
return data;
})
};
axios(options).then(res => showOutput(res));
}
// ERROR HANDLING
function errorHandling() {
axios
.get('https://jsonplaceholder.typicode.com/todoss', {
// validateStatus: function(status) {
// return status < 500; // Reject only if status is greater or equal to 500
// }
})
.then(res => showOutput(res))
.catch(err => {
if (err.response) {
// Server responded with a status other than 200 range
console.log(err.response.data);
console.log(err.response.status);
console.log(err.response.headers);
if (err.response.status === 404) {
alert('Error: Page Not Found');
}
} else if (err.request) {
// Request was made but no response
console.error(err.request);
} else {
console.error(err.message);
}
});
}
// CANCEL TOKEN
function cancelToken() {
const source = axios.CancelToken.source();
axios
.get('https://jsonplaceholder.typicode.com/todos', {
cancelToken: source.token
})
.then(res => showOutput(res))
.catch(thrown => {
if (axios.isCancel(thrown)) {
console.log('Request canceled', thrown.message);
}
});
if (true) {
source.cancel('Request canceled!');
}
}
// INTERCEPTING REQUESTS & RESPONSES
axios.interceptors.request.use(
config => {
console.log(
`${config.method.toUpperCase()} request sent to ${
config.url
} at ${new Date().getTime()}`
);
return config;
},
error => {
return Promise.reject(error);
}
);
// AXIOS INSTANCE
const axiosInstance = axios.create({
// Other custom settings
baseURL: 'https://jsonplaceholder.typicode.com'
});
// axiosInstance.get('/comments').then(res => showOutput(res));
// Show output in browser
function showOutput(res) {
document.getElementById('res').innerHTML = `
<div class="card card-body mb-4">
<h5>Status: ${res.status}</h5>
</div>
<div class="card mt-3">
<div class="card-header">
Headers
</div>
<div class="card-body">
<pre>${JSON.stringify(res.headers, null, 2)}</pre>
</div>
</div>
<div class="card mt-3">
<div class="card-header">
Data
</div>
<div class="card-body">
<pre>${JSON.stringify(res.data, null, 2)}</pre>
</div>
</div>
<div class="card mt-3">
<div class="card-header">
Config
</div>
<div class="card-body">
<pre>${JSON.stringify(res.config, null, 2)}</pre>
</div>
</div>
`;
}
// Event listeners
document.getElementById('get').addEventListener('click', getTodos);
document.getElementById('post').addEventListener('click', addTodo);
document.getElementById('update').addEventListener('click', updateTodo);
document.getElementById('delete').addEventListener('click', removeTodo);
document.getElementById('sim').addEventListener('click', getData);
document.getElementById('headers').addEventListener('click', customHeaders);
document
.getElementById('transform')
.addEventListener('click', transformResponse);
document.getElementById('error').addEventListener('click', errorHandling);
document.getElementById('cancel').addEventListener('click', cancelToken);
================================================
FILE: start.js
================================================
// GET REQUEST
function getTodos() {
console.log('GET Request');
}
// POST REQUEST
function addTodo() {
console.log('POST Request');
}
// PUT/PATCH REQUEST
function updateTodo() {
console.log('PUT/PATCH Request');
}
// DELETE REQUEST
function removeTodo() {
console.log('DELETE Request');
}
// SIMULTANEOUS DATA
function getData() {
console.log('Simultaneous Request');
}
// CUSTOM HEADERS
function customHeaders() {
console.log('Custom Headers');
}
// TRANSFORMING REQUESTS & RESPONSES
function transformResponse() {
console.log('Transform Response');
}
// ERROR HANDLING
function errorHandling() {
console.log('Error Handling');
}
// CANCEL TOKEN
function cancelToken() {
console.log('Cancel Token');
}
// INTERCEPTING REQUESTS & RESPONSES
// AXIOS INSTANCES
// Show output in browser
function showOutput(res) {
document.getElementById('res').innerHTML = `
<div class="card card-body mb-4">
<h5>Status: ${res.status}</h5>
</div>
<div class="card mt-3">
<div class="card-header">
Headers
</div>
<div class="card-body">
<pre>${JSON.stringify(res.headers, null, 2)}</pre>
</div>
</div>
<div class="card mt-3">
<div class="card-header">
Data
</div>
<div class="card-body">
<pre>${JSON.stringify(res.data, null, 2)}</pre>
</div>
</div>
<div class="card mt-3">
<div class="card-header">
Config
</div>
<div class="card-body">
<pre>${JSON.stringify(res.config, null, 2)}</pre>
</div>
</div>
`;
}
// Event listeners
document.getElementById('get').addEventListener('click', getTodos);
document.getElementById('post').addEventListener('click', addTodo);
document.getElementById('update').addEventListener('click', updateTodo);
document.getElementById('delete').addEventListener('click', removeTodo);
document.getElementById('sim').addEventListener('click', getData);
document.getElementById('headers').addEventListener('click', customHeaders);
document
.getElementById('transform')
.addEventListener('click', transformResponse);
document.getElementById('error').addEventListener('click', errorHandling);
document.getElementById('cancel').addEventListener('click', cancelToken);
gitextract_klvj9h2b/ ├── README.md ├── index.html ├── main.js └── start.js
SYMBOL INDEX (20 symbols across 2 files)
FILE: main.js
function getTodos (line 6) | function getTodos() {
function addTodo (line 26) | function addTodo() {
function updateTodo (line 37) | function updateTodo() {
function removeTodo (line 48) | function removeTodo() {
function getData (line 56) | function getData() {
function customHeaders (line 67) | function customHeaders() {
function transformResponse (line 89) | function transformResponse() {
function errorHandling (line 106) | function errorHandling() {
function cancelToken (line 134) | function cancelToken() {
function showOutput (line 177) | function showOutput(res) {
FILE: start.js
function getTodos (line 2) | function getTodos() {
function addTodo (line 7) | function addTodo() {
function updateTodo (line 12) | function updateTodo() {
function removeTodo (line 17) | function removeTodo() {
function getData (line 22) | function getData() {
function customHeaders (line 27) | function customHeaders() {
function transformResponse (line 32) | function transformResponse() {
function errorHandling (line 37) | function errorHandling() {
function cancelToken (line 42) | function cancelToken() {
function showOutput (line 51) | function showOutput(res) {
Condensed preview — 4 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (10K chars).
[
{
"path": "README.md",
"chars": 178,
"preview": "# Axios Crash Course\r\n\r\n> These are the files for the YouTube Axios crash course.\r\n\r\n\"start.js\" is just the event listen"
},
{
"path": "index.html",
"chars": 1580,
"preview": "<!DOCTYPE html>\n<html lang=\"en\">\n <head>\n <meta charset=\"UTF-8\" />\n <meta name=\"viewport\" content=\"width=device-w"
},
{
"path": "main.js",
"chars": 5603,
"preview": "// AXIOS GLOBALS\naxios.defaults.headers.common['X-Auth-Token'] =\n 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM"
},
{
"path": "start.js",
"chars": 2212,
"preview": "// GET REQUEST\nfunction getTodos() {\n console.log('GET Request');\n}\n\n// POST REQUEST\nfunction addTodo() {\n console.log"
}
]
About this extraction
This page contains the full source code of the bradtraversy/axios-crash GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 4 files (9.3 KB), approximately 2.6k tokens, and a symbol index with 20 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.
Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.