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>