Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

262
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda