Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Im making an API in which I'm calling GET to the musicBrainz API.

(我正在制作一个API,在其中我将GET调用到musicBrainz API。)

Im using node.js and express.

(我正在使用node.js并表达。)

My requests are denied because they lack a User-Agent (which is according to their rules: https://musicbrainz.org/doc/XML_Web_Service/Rate_Limiting )

(我的请求被拒绝,因为它们缺少用户代理 (根据他们的规则: https : //musicbrainz.org/doc/XML_Web_Service/Rate_Limiting ))

My code:

(我的代码:)


const https = require('https');

    var callmbapi = function(mbid, callback, res) {
    var artistdata = '';
    const mburl = 'https://musicbrainz.org/ws/2/artist/';
    https.get(mburl + mbid + '?inc=release-groups&fmt=json', (resp) => {

        // A chunk of data has been recieved.
         resp.on('data', (chunk) => {
           artistdata += chunk;
        });
        resp.on('end', function () {
            console.log(artistdata);
        });

        }).on("error", (err) => {
            console.log("Error: " + err.message);
    });
};

This request worked before I reached the limit on requests without a User-Agent.

(在我达到没有User-Agent的请求的限制之前,此请求已经生效。)

I read somewhere that I was supposed to have option which I send with the request, and have also tried:

(我在某个地方读到了应该带有随请求发送的选项,并且还尝试了:)


const https = require('https');

const options = {
    headers: { "User-Agent": "<my user agent>" }
};

var callmbapi = function(mbid, callback, res) {
    var artistdata = '';
    const mburl = 'https://musicbrainz.org/ws/2/artist/';
    https.get(options, mburl + mbid + '?inc=release-groups&fmt=json', (resp) => {

        // A chunk of data has been recieved.
         resp.on('data', (chunk) => {
           artistdata += chunk;
        });
        resp.on('end', function () {
            console.log(artistdata);
        });

        }).on("error", (err) => {
            console.log("Error: " + err.message);
    });
};

But this does not work.

(但这是行不通的。)

My question is How do I add a User-Agent to my request?

(我的问题是如何向请求添加用户代理?)

I am completely new to this, and have been trying to find out by myself the last 1.5h but seems that this is so basic that it is never described anywhere.

(我对此是完全陌生的,并且一直在尝试自己找出最后1.5小时,但似乎这是如此基础,以至于从未在任何地方进行描述。)

  ask by ojaoweir translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
358 views
Welcome To Ask or Share your Answers For Others

1 Answer

hm, according to npm, https wasn't updated in five years.

(嗯,据npm称, https在五年内没有更新。)

So let's assume, you would use something newer like axios .

(因此,让我们假设,您将使用像axios这样的较新版本。)

Here, the request would be like this:

(在这里,请求将如下所示:)

const callmbapi = function (mbid) {
  const axios = require('axios');
  return axios
    .get('https://musicbrainz.org/ws/2/artist/' + mbid + '?inc=release-groups&fmt=json', { "User-Agent": "<my user agent>" })
    .catch(function (err) {
      console.log("Error: " + err.message); 
    });
  }
}

Note, that this returns a Promise , ie you need to call .then(function (artistdata) { /* ... */ }) on the function (instead of using a callback).

(请注意,这将返回Promise ,即您需要在.then(function (artistdata) { /* ... */ })上调用.then(function (artistdata) { /* ... */ }) (而不是使用回调)。)

With a more modern Node.js, you could use await instead:

(使用更现代的Node.js,您可以改用await :)

const callmbapi = async function (mbid) {
  const axios = require('axios');
  try {
    return axios.get('https://musicbrainz.org/ws/2/artist/' + mbid + '?inc=release-groups&fmt=json', { "User-Agent": "<my user agent>" })
  } catch(err) {
    console.log("Error: " + err.message); 
  }
}

Here you would const artistdata = await callmbapi(mbid) your data.

(在这里,您将const artistdata = await callmbapi(mbid)您的数据。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...