简体   繁体   English

node.js https 获取请求

[英]node.js https get request

I am trying to make an http request in my backend node.js web app.我正在尝试在我的后端 node.js web 应用程序中发出 http 请求。 I can make sucesfull http requests on front end web javascript files like this:我可以在前端 web javascript 文件上发出成功的 http 请求,如下所示:

            $.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并遵循本指南:https://nodejs.dev/learn/make-an-http-post-request-using-nodejs

I have been trying to make the same request in node.js backend:我一直在尝试在 node.js 后端发出相同的请求:

    //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?我设置 url/路径设置的方式有问题吗?

Node https module have a separate option named protocol .节点 https 模块有一个名为protocol的单独选项。 So in the request option, you should not include https:// or http:// .因此,在请求选项中,您不应包含https://http://

Your options variable will become:您的选项变量将变为:

const options = {
  hostname: 'api.spotify.com',
  path: '/v1/artists/1XqqyIQYMonHgllb1uysL3/albums',
  method: 'GET',
  headers: {
    'Authorization': 'Bearer ' + accessToken,
  },
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM