search

Create Method ( Function ) in Axios

Comments:
Likes:
Shares:
Best Division

Best Division  @bestdivision

Published On - Last Updated -

Create method in Axios

1: Installing Axios.

npm install axios

2: Create a file axios.js and paste the following command.

import axios from "axios";

const instance = axios.create({
    baseURL: 'http://www.example.com/',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
});

export default instance;

3: Using Axios to make a GET request.

a) Open App.js ( the file you want to make call from ).

b) Import axios file you made in step 2. 

import React from 'react'
import axios from '../../axios';

function App() {
	const makeGetRequest = async () => {
        let response = await axios({
            method: 'get',
            url: `/users/1`
        });
        console.log(response);

    }
    makeGetRequest();
    return (
        <div>
            
        </div>
    )
}

export default App

So, the above function will make a get request and log the response in console.

Similar Blogs
0 Commentssort Sort By

@abcd 1 days ago

Aquí los que apoyamos a Los del limit desde sus inicios..

dislike
reply
sdfsdf