Post file as raw body in AXIOS NodeJS. I tried many ways to achieve this but none of them worked.
What i have tried ?
var file = fs.readFileSync("a.jpg"); var body = await axios({ method: 'POST', url : "myUrl", data : file }); var file = fs.readFileSync("a.jpg").toString(); var body = await axios({ method: 'POST', url : "myUrl", data : file }); var file = fs.readFileSync("a.jpg",{encoding:"utf8"}).toString(); var body = await axios({ method: 'POST', url : "myUrl", data : file }); var file = fs.readFileSync("a.jpg"); file = Buffer.from(file).toString('utf8') var body = await axios({ method: 'POST', url : "myUrl", data : file }); var file = fs.createReadStream("a.jpg"); var body = await axios({ method: 'POST', url : "myUrl", data : file }); But none of them worked as i wanted.
Actual working example from JQuery AJAX in Browser
var fileupload = $("#inpFile")[0]; var file = fileupload.files[0]; $.ajax({ url: "https://hookb.in/b9gqlwbZeaT3DDogQ7Om", type: 'POST', success: function (response) { DisplayMessage(response); }, data: file, contentType: false, processData: false });