1const axios = require('axios');
2
3// Make a request for a user with a given ID
4axios.get('/user?ID=12345')
5 .then(function (response) {
6 // handle success
7 console.log(response);
8 })
9 .catch(function (error) {
10 // handle error
11 console.log(error);
12 })
13 .then(function () {
14 // always executed
15 });
16
17// Optionally the request above could also be done as
18axios.get('/user', {
19 params: {
20 ID: 12345
21 }
22 })
23 .then(function (response) {
24 console.log(response);
25 })
26 .catch(function (error) {
27 console.log(error);
28 })
29 .then(function () {
30 // always executed
31 });
32
33// Want to use async/await? Add the `async` keyword to your outer function/method.
34async function getUser() {
35 try {
36 const response = await axios.get('/user?ID=12345');
37 console.log(response);
38 } catch (error) {
39 console.error(error);
40 }
41}
1const axios = require('axios');
2axios.post('/user', {
3 firstName: 'Fred',
4 lastName: 'Flintstone'
5 })
6 .then(function (response) {
7 console.log(response);
8 })
9 .catch(function (error) {
10 console.log(error);
11 });
1import axios from 'axios';
2
3let one = "https://api.storyblok.com/v1/cdn/stories/health?version=published&token=wANpEQEsMYGOwLxwXQ76Ggtt"
4let two = "https://api.storyblok.com/v1/cdn/datasources/?token=wANpEQEsMYGOwLxwXQ76Ggtt"
5let three = "https://api.storyblok.com/v1/cdn/stories/vue?version=published&token=wANpEQEsMYGOwLxwXQ76Ggtt"
6
7const requestOne = axios.get(one);
8const requestTwo = axios.get(two);
9const requestThree = axios.get(three);
10
11axios.all([requestOne, requestTwo, requestThree]).then(axios.spread((...responses) => {
12 const responseOne = responses[0]
13 const responseTwo = responses[1]
14 const responesThree = responses[2]
15 // use/access the results
16})).catch(errors => {
17 // react on errors.
18})
19
1const req = async () => {
2 const response = await axios.get('https://dog.ceo/api/breeds/list/all')
3 console.log(response)
4}
5req() // Calling this will make a get request and log the response.
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 });
1Axios is a Promise-based HTTP client for JavaScript which can be used in your
2front-end application and in your Node. js backend. By using Axios it's easy
3to send asynchronous HTTP request to REST endpoints and perform CRUD
4operations.