xxxxxxxxxx
// Make a request for a user with a given ID
axios.get('/user?ID=12345')
.then(function (response) {
// handle success
console.log(response);
})
.catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
});
// Optionally the request above could also be done as
axios.get('/user', {
params: {
ID: 12345
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
})
.then(function () {
// always executed
});
// Want to use async/await? Add the `async` keyword to your outer function/method.
async function getUser() {
try {
const response = await axios.get('/user?ID=12345');
console.log(response);
} catch (error) {
console.error(error);
}
}
xxxxxxxxxx
const axios = require('axios');
axios.post('/user', {
firstName: 'Fred',
lastName: 'Flintstone'
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
xxxxxxxxxx
const req = async () => {
const response = await axios.get('https://dog.ceo/api/breeds/list/all')
console.log(response)
}
req() // Calling this will make a get request and log the response.
xxxxxxxxxx
const axios = require('axios');
// Make a request for a user with a given ID
axios.get('/user?ID=12345')
.then(function (response) {
// handle success
console.log(response);
})
.catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
});
xxxxxxxxxx
const axios = require('axios').default;
// Make a request for a user with a given ID
axios.get('/user?ID=12345')
.then(function (response) {
// handle success
console.log(response);
})
.catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
});
// Optionally the request above could also be done as
axios.get('/user', {
params: {
ID: 12345
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
})
.then(function () {
// always executed
});
// Want to use async/await? Add the `async` keyword to your outer function/method.
async function getUser() {
try {
const response = await axios.get('/user?ID=12345');
console.log(response);
} catch (error) {
console.error(error);
}
}
xxxxxxxxxx
import axios from "axios";
axios
.get('https://jsonplaceholder.typicode.com/todos')
.then((res) => {
const data = res.data.json();
})
.catch((err) => {
console.log(err);
});
xxxxxxxxxx
import axios from "axios";
// 2 ways of doing it.
// using axios(options)
await axios({ // You can use async/await or .then()
url: 'URL'
method: 'POST'
headers:{}
data:{} //this is the body of the request
})
// Using axios.post('URL', data, options)
await axios.post('URL', { username: 'test', password: '123456' }, { headers:{} })
xxxxxxxxxx
const axios = require('axios');
async function makeGetRequest() {
let res = await axios.get('http://webcode.me');
let data = res.data;
console.log(data);
}
makeGetRequest();
xxxxxxxxxx
try {
const response = await axios.post('https://movie-app-3073.herokuapp.com/login/', {
Username: username,
Password: password
});
const data = response.data;
props.onLoggedIn(data);
} catch (error) {
console.log('This user cannot be found');
}
xxxxxxxxxx
axios.post('https:sample-endpoint.com/user', {
Name: 'Fred',
Age: '23'
})
.then(function (response) {
console.log(response);
})