Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

269
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda