Created
July 9, 2018 13:52
-
-
Save folt/5053e28e0f36bd90f2f594e1f90fef8d to your computer and use it in GitHub Desktop.
axios + FormData + multipart/form-data upload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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