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

188
Visualizações
node.js https get request

I am trying to make an http request in my backend node.js web app. I can make sucesfull http requests on front end web javascript files like this:

            $.ajax({
                url: `https://api.spotify.com/v1/artists/${artistId}/albums`,
                type: 'GET',
                contentType: 'application/json',
                headers: {
                    'Authorization': 'Bearer ' + globalAccesToken,
                },
                data: {
                    offset: offset,
                }
            }).done(function callback(response) {
           
                    resolve(response.items)
               
            }).fail(async function (error) {
getArtistAlbums(artistId, offset, onlyReturnTracks))
            });

And from following this guide: https://nodejs.dev/learn/make-an-http-post-request-using-nodejs

I have been trying to make the same request in node.js backend:

    //get access token
    let accessToken = await spotifyAuth.getAccessToken();
    
    //create data
    const data = JSON.stringify({
      offset: 0,
    })

    //create options data obj
    const options = {
      hostname: 'https://api.spotify.com',
      path: '/v1/artists/1XqqyIQYMonHgllb1uysL3/albums',

      //url: 'https://api.spotify.com/v1/artists/1XqqyIQYMonHgllb1uysL3/albums',
      method: 'GET',
      headers: {
        'Authorization': 'Bearer ' + accessToken,
      },
    }

    const req = https.request(options, res => {
      console.log(`proxyiprequest() statusCode: ${res.statusCode}`)
      res.on('data', d => {
        process.stdout.write(d)
      })
    })
    
    req.on('error', error => {
      console.error('proxyiprequest() err=', error)
    })
    
    req.write(data)
    req.end()

But running the above request ends in this err:

proxyiprequest() err= { Error: getaddrinfo ENOTFOUND https://api.spotify.com https://api.spotify.com:443
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:56:26)
  errno: 'ENOTFOUND',
  code: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: 'https://api.spotify.com',
  host: 'https://api.spotify.com',
  port: 443 }

Am I doing something wrong with how I have set up my url/path settings?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Node https module have a separate option named protocol . So in the request option, you should not include https:// or http://.

Your options variable will become:

const options = {
  hostname: 'api.spotify.com',
  path: '/v1/artists/1XqqyIQYMonHgllb1uysL3/albums',
  method: 'GET',
  headers: {
    'Authorization': 'Bearer ' + accessToken,
  },
}
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