Skip to content

Instantly share code, notes, and snippets.

@folt
Created July 9, 2018 13:52
Show Gist options
  • Save folt/5053e28e0f36bd90f2f594e1f90fef8d to your computer and use it in GitHub Desktop.
Save folt/5053e28e0f36bd90f2f594e1f90fef8d to your computer and use it in GitHub Desktop.
axios + FormData + multipart/form-data upload
const axios = require('axios');
const FormData = require('form-data');
const fs = require('fs');
let form = new FormData();
let axiosConfig = {
headers: form.getHeaders(
{
'authorization': 'Bearer token123',
'cache-control': 'no-cache',
}
),
};
form.append('file', fs.createReadStream(__dirname + '/test.png'));
form.append('profile_id', '5eb91652-0000-1111-2222-26fc333e1331');
axios.post('http://localhost:8080/images/', form, axiosConfig).then(response => {
console.log(response.data);
}).catch(error => {
if (error.response) {
console.log(error.response.data);
}
console.log(error.message);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment