Reputation: 5131
Here's the flow that I am trying to achieve:
1) User uploads an audio file to server1
2) Server1 receives that audio file and sends it to server2 in different domain
3) Server2 converts audio file to text
4) Server2 responds back to server1 with text
5) Server1 displays the text to the user
Speech to text conversion on server2 is done. I am stuck on sending the audio file and waiting for response. I know how to send request using GET, but I don't think I can use it with audio files.
How can I send an audio file to another server using node.js?
I am pretty new to node.js, so any help would be appreciated. Thanks.
UPDATE:
Server2 uses REST API and expects the file to be POST'ed.
Upvotes: 0
Views: 3030
Reputation: 2226
This is a good use case for messaging. I'd suggest a flow like this:
Some weaknesses of direct HTTP communication between Server A and Server B are:
Upvotes: 0
Reputation: 156464
It totally depends on how the remote server expects to receive the audio file. Assuming that it has some sort of RESTful web service interface whereby the file contents are POST'ed to some URL, you might be able to do something like this:
fs.readFile('/path/to/my/audiofile.wav', function (err, data) {
if (err) throw err;
var options = {
host: 'remotehost.com',
path: '/upload/wav',
method: 'POST',
headers: { 'Content-Type': 'audio/wav' }
};
var req = http.request(options, function(res) {
// Handle a successful response here...
});
req.on('error', function(e) {
// Handle an error response here...
});
// Write the audio data in the request body.
req.write(data);
req.end();
});
Again, it completely depends on how the server wants you to send the data. It may expect a completely different protocol (or method, or path), authentication, encoding, or any number of specifics which would completely change the viability of my sample answer.
Upvotes: 2