1// Send a POST request
2axios({
3 method: 'post',
4 url: '/user/12345',
5 data: {
6 firstName: 'Fred',
7 lastName: 'Flintstone'
8 }
9});
1const axios = require('axios');
2
3async function makeGetRequest() {
4
5 let res = await axios.get('http://webcode.me');
6
7 let data = res.data;
8 console.log(data);
9}
10
11makeGetRequest();
12
1// Make a request for a user with a given ID
2axios.get('/user?ID=12345')
3 .then(function (response) {
4 console.log(response);
5 })
6 .catch(function (error) {
7 console.log(error);
8 });
9
10// Optionally the request above could also be done as
11axios.get('/user', {
12 params: {
13 ID: 12345
14 }
15 })
16 .then(function (response) {
17 console.log(response);
18 })
19 .catch(function (error) {
20 console.log(error);
21 });
1const body = { a: 10 };
2
3axios.post('/save', body);
4
5// Axios automatically serializes JavaScript objects to JSON
6// when passed to the axios.post function as the second parameter.
7// This eliminates the need to serialize POST bodies to JSON.