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}
1axios.get('url')
2 .then((response) => {
3
4 // Code for handling the response
5 })
6 .catch((error) => {
7
8 // Code for handling the error
9 })
1 const { data } = await axios({
2 method: 'post',
3 url: `${process.env.REACT_APP_API_URL}/api/send-otp`,
4 data: { phone: phoneNumber },
5 //withCredentials: true,
6 })
7 console.log(data)
1// npm install axios
2const axios = require('axios');
3
4// Make a request for a user with a given ID
5axios.get('/user?ID=12345')
6 .then(function (response) {
7 // handle success
8 console.log(response);
9 })
10 .catch(function (error) {
11 // handle error
12 console.log(error);
13 })
14 .then(function () {
15 // always executed
16 });
17
18// Optionally the request above could also be done as
19axios.get('/user', {
20 params: {
21 ID: 12345
22 }
23 })
24 .then(function (response) {
25 console.log(response);
26 })
27 .catch(function (error) {
28 console.log(error);
29 })
30 .then(function () {
31 // always executed
32 });
33
34// Want to use async/await? Add the `async` keyword to your outer function/method.
35async function getUser() {
36 try {
37 const response = await axios.get('/user?ID=12345');
38 console.log(response);
39 } catch (error) {
40 console.error(error);
41 }
42}
1import axios from "axios";
2
3const API_URL = "http://localhost:8080/api/auth/";
4
5const register = (username, email, password) => {
6 return axios.post(API_URL + "signup", {
7 username,
8 email,
9 password,
10 });
11};
12
13const login = (username, password) => {
14 return axios
15 .post(API_URL + "signin", {
16 username,
17 password,
18 })
19 .then((response) => {
20 if (response.data.accessToken) {
21 localStorage.setItem("user", JSON.stringify(response.data));
22 }
23
24 return response.data;
25 });
26};
27
28const logout = () => {
29 localStorage.removeItem("user");
30};
31
32export default {
33 register,
34 login,
35 logout,
36};
1//npm install axios
2
3const axios = require('axios').default;
4async function getUser() {
5 try {
6 const response = await axios.get('/user?ID=12345');
7 console.log(response);
8 } catch (error) {
9 console.error(error);
10 }
11}