Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

261
Views
How can I download audio file using npm request in nodejs

I am having this piece of code which gives me some raw data

        requestPromisePost(options) {
          return new Promise((resolve, reject) => {
            request(options, function (error, response,dfdf) {
              if (error) return reject(error);
              return resolve(response.body);
           });
         });
       }


        const auth = new Buffer('apiKey' + ':' + '').toString('base64');
        const options = {
            'method': 'GET',
            'url': 'myUrl',
            'headers': {
                'Content-Type': 'application/json',
                'Authorization': `Basic ${auth}`
            }
        }
        const result = await this.requestPromisePost(options);
        console.log(result)

Which gives me this data

��Y�;��;��\n' +
    '�\x01��b�H�\x13ZP�H��\t�r�\x17(\x1C`��BR�6q��A|��m�O���l�\x0F\r��@��\x15!r�X���\x05�A�K:6(ӻ���ܖ&�6r�[[_�?\x7FpHH�Dr��q��\x18%2\x0B�\x14�@\x01\x03\x12�;�\x04yB` \x01Jʠfb�\b\tQ\t;U�m~;\x17m�FHlq�ą\rΑ|J����B��\x17�~�X��\x1A\x16f��W�\x0E/\x17y&}J�4

Which is an audio file. I have to download file. Kindly help.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Here is the solution

const filename = "myAudio.mp3"

const options = {
    'method': 'GET',
    'url': YOUR_AUDIO_URL_HERE,
    'headers': {
        'Content-Type': 'application/json',
    },
    encoding: null // <-- This is so important! It means binary data.
}

request(options, function (err, resp, data) {
    // Here, data is the argument which contains binary data of the file.
    if (err || resp.statusCode != 200) {
        console.log(err, resp.body);
    } else {
        try {
            fs.writeFileSync(filename, data, { encoding: 'binary'});
            console.log(`Output audio file: ${filename}, length: ${data.length} byte(s)`);
        } catch (e) {
            console.log(e.message);
        }
    }
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!