I am working on a nodejs project which needs to get a response by making a POST request to the API in which I need to send an audio file (audio.ogg) in the body of request. In 'postman' we can do something like this by selecting the file from local and make request.
How can I do the same with my Nodejs application using an npm package like 'request'?
My code so far is quoted here:
var request = require('request');
var fs = require('fs');
var path = require('path');
router.get('/', function(req, res, next) {
var options = {
method: 'post',
body: {
'language': "<language>",
'audio_file':path.join(__dirname, 'audio.ogg')
}, // Javascript object
json: true,
url: "<API>",
headers: {
'Authorization': "<token>",
'ContentType': "application/x-www-form-urlencoded"
}
}
request(options, function (err, res, body) {
if (err) {
console.log('Error :', err)
return
}
console.log(' Body :', body)
});
// res.render('index', { title: 'Express' });
});