xxxxxxxxxx
import axios from "axios";
/// GET REQUEST
/// Example 1 : Start
// Simple Get Request using Axios
function getData() {
return axios.get("url");
}
/// Example 1 : End
///
/// Example 2 : Start
// Get Request with Params
getData({ page: page, limit: 5, sort: "name", order: "ASC" })
.then((res) => {
// console.log(res);
// op -> {data: Array(5), status: 200, statusText: "OK", headers: Object, config: Object…}
// console.log(res.data);
// op -> [Object, Object, Object, Object, Object]
setData(res.data);
})
.catch((err) => console.log(err));
function getData(params = {}) {
// console.log(params); // op -> {page: 1, limit: 5, sort: "name", order: "ASC"}
return axios.get("url", {
params: {
_page: params.page,
_limit: params.limit,
_sort: params.sort,
_order: params.order
}
});
}
/// Example 2 : End
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
import axios from 'axios';
axios.get('/user', {
params: {
ID: 12345
}
})
.then(function (response) {
console.log(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 qs from 'qs';
const data = { 'bar': 123 };
const options = {
method: 'POST',
headers: { 'content-type': 'application/x-www-form-urlencoded' },
data: qs.stringify(data),
url,
};
axios(options);
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');
const res = await axios.get('https://httpbin.org/get', { params: { answer: 42 } });
res.constructor.name; // 'Object', means `res` is a POJO
// `res.data` contains the parsed response body
res.data; // { args: { answer: 42 }, ... }
res.data instanceof Object; // true
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
// 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);
}
}