xxxxxxxxxx
const res = await axios.put('https://httpbin.org/put', { hello: 'world' });
res.data.headers['Content-Type'];
Node.js - Axios Instance
xxxxxxxxxx
const axios = require('axios')
const api = axios.create({
baseURL: "https://example.com",
withCredentials: true,
headers: {
'Content-type': 'application/json',
Accept: 'application/json',
},
});
// List of all the endpoints
const sendOtp = (data) => api.post('/api/send-otp', data);
const verifyOtp = (data) => api.post('/api/verify-otp', data);
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 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
const axios = require('axios');
async function makeGetRequest() {
let res = await axios.get('http://webcode.me');
let data = res.data;
console.log(data);
}
makeGetRequest();
xxxxxxxxxx
import axios from "axios";
/// POST REQUEST
addCity(data)
.then((res) => {
console.log(res);
alert("success");
});
export function addCity(data = {}) {
// console.log(data);
// {name: "mysore", population: 11111, country: "India"}
return axios.post("URL", {
name: data.name,
population: data.population,
country: data.country
});
}
xxxxxxxxxx
import axios from 'axios';
axios.get('/user_login', {
params: {
username: 'john1904',
}
})
.then(function (response) {
console.log(response);
})
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