axios post
const axios = require('axios');
axios.post('/user', {
firstName: 'Fred',
lastName: 'Flintstone'
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
axios post
const axios = require('axios');
axios.post('/user', {
firstName: 'Fred',
lastName: 'Flintstone'
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
axios get
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
});
// Optionally the request above could also be done as
axios.get('/user', {
params: {
ID: 12345
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
})
.then(function () {
// always executed
});
// Want to use async/await? Add the `async` keyword to your outer function/method.
async function getUser() {
try {
const response = await axios.get('/user?ID=12345');
console.log(response);
} catch (error) {
console.error(error);
}
}
Axios get
const axios = require('axios');
async function makeGetRequest() {
let res = await axios.get('http://webcode.me');
let data = res.data;
console.log(data);
}
makeGetRequest();
axios.post
<script>
export default {
data(){
return {
urlBase: 'http://localhost:8000/api/v1/marca',
nomeMarca: '',
arquivoImagem: []
}
},
methods: {
carregarImagem(e){
this.arquivoImagem = e.target.files // object array
},
guardar(){
// Warning : e.target.files is an array of objects
console.log(this.nomeMarca , this.arquivoImagem[0])
/* axios.post( <url>,
<request_content>,
<request_config>
)
*/
/*
When we are talking about http requests for the backend
of our application that involve sending files,
we need to set the body of our request to
being of the form-data type.
*/
// program a form via javascript just like the POSTMAN form
let formData = new FormData()
formData.append('nome', this.nomeMarca)
formData.append('imagem', this.arquivoImagem[0])
let config = {
headers: {
'Content-Type': 'multipart/form-data',
'Accept': 'application/json'
}
}
axios.post(this.urlBase, formData, config)
.then(response => {
console.log(response)
})
.catch(errors => {
console.log(errors)
})
}
}
}
</script>
what is axios used for
Axios is a Promise-based HTTP client for JavaScript which can be used in your
front-end application and in your Node. js backend. By using Axios it's easy
to send asynchronous HTTP request to REST endpoints and perform CRUD
operations.
Axios get
const axios = require('axios');
async function makeRequest() {
const config = {
method: 'get',
url: 'http://webcode.me'
}
let res = await axios(config)
console.log(res.status);
}
makeRequest();
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us