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