Axios is a simple promise based HTTP client for the browser and node.js. Axios
provides a simple to use library in a small package with a very extensible.
Just like javascript fetching functionalities, axios provides the the fetching
data functionality from api.
Install axios using npm:
$ npm install axios
Install axios Using bower:
$ bower install axios
Install axios using yarn:
$ yarn add axios
Import axios as follows:
const axios = require('axios').default;
// Make a get request with axios sample code
axios.get('/user', {
params: {
ID: 12345
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
You can start learning on axios from the below website:
https://axios-http.com/docs/intro
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.
const axios = require('axios');
async function makeGetRequest() {
let res = await axios.get('http://webcode.me');
let data = res.data;
console.log(data);
}
makeGetRequest();
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
});
import axios from 'axios';
async makeRequest() {
try {
const response = await axios.post('your_request_url_here', {
// Enter your body parameters here
});
}
catch(e) {
// Handle your error here
}
}
// Axios returns a promise and hence you can use .then(), .catch for error
// handling if you don't want to use async/await(use try/catch for error
// handling)
const onSubmit = async (FormData) => {
FormData.bFlats = meters;
try {
let res = await axios({
method: 'post',
url: 'http://localhost:5000/api/v1/buildings',
data: FormData
});
console.log(res.data);
if (res.data.status === 'success') {
alert("Successfully Inserted");
reset();
}
} catch (error) {
console.log(error.response.data.message); // this is the main part. Use the response property from the error object
}
}
async function send()
{
let data = new FormData();
data.append("title", "title ???");
data.append("note", "note");
data.append("csrfmiddlewaretoken", '{{csrf_token}}') // 3
let res = await axios.post("/blog/third", data);
console.log(res.data.my_data);
}
def third(request):
if request.method=="POST":
title = request.POST["title"]
note = request.POST["note"]
data = {
'my_data': title
}
return JsonResponse(data)
#much simpler than fetch
Instance methods
The available instance methods are listed below. The specified config will be merged with the instance config.
axios#request(config)
axios#get(url[, config])
axios#delete(url[, config])
axios#head(url[, config])
axios#options(url[, config])
axios#post(url[, data[, config]])
axios#put(url[, data[, config]])
axios#patch(url[, data[, config]])
axios#getUri([config])