I need to send file via axios POST request to a remote API with the following parameter : 'operation' => 'x' and 'files' => the file itself.
The file is a pdf file. I tried this:
let formData = new FormData();
formData.append('operation', 'x');
formData.append('files', this.file);
axios({
url: 'http://xxxxx',
method: 'POST',
data: formData,
headers: {
Accept: 'application/json',
'Content-Type': 'multipart/form-data'
}
}).then(response => {
console.log(response);
})
.catch(error => {
console.log(error);
});
}
But I cannot get it work. I trieed also using set method on formData but it doesn't work aswell. Is there something I am making wrong? PS. I tried with POSTMan and it works so it is not an API problem. Thank you.
this.file
? The file needs to be a file form element previously selected by the user, look at this example github.com/axios/axios/blob/master/examples/upload/… – tato