admin管理员组文章数量:1316980
Im making an API in which I'm calling GET to the musicBrainz API. Im using node.js and express.
My requests are denied because they lack a User-Agent (which is according to their rules: )
My code:
const https = require('https');
var callmbapi = function(mbid, callback, res) {
var artistdata = '';
const mburl = '/';
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.
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.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 pletely 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.
Im making an API in which I'm calling GET to the musicBrainz API. Im using node.js and express.
My requests are denied because they lack a User-Agent (which is according to their rules: https://musicbrainz/doc/XML_Web_Service/Rate_Limiting)
My code:
const https = require('https');
var callmbapi = function(mbid, callback, res) {
var artistdata = '';
const mburl = 'https://musicbrainz/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.
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/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 pletely 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.
Share Improve this question asked Nov 28, 2019 at 21:00 ojaoweirojaoweir 551 silver badge4 bronze badges 2-
Have you checked the Node.js documentation for the
https
module? It may contain the information you are looking for. Also, what do you mean that the second attempt "does not work"? What does not work? – Robert Rossmann Commented Nov 28, 2019 at 22:16 - I checked the documentation, but do not understand how I actually add the agent, or what format it should be. If I try the second option i get this error: ``` TypeError [ERR_INVALID_ARG_TYPE]: The "listener" argument must be of type Function. Received type string ``` The only thing changed is that i added options to the https.get() – ojaoweir Commented Nov 29, 2019 at 6:39
2 Answers
Reset to default 6With the Node.js http(s) module, the function arguments are (url, options, callback):
import https from 'https';
const options = {
headers: {
'User-Agent': 'some app v1.3 ([email protected])',
}
};
let body = '';
https.get('https://httpbin/headers', options, response => {
console.log('status code:', response.statusCode);
response.on('data', chunk => body += chunk);
response.on('end', () => console.log(body + "\n"));
});
PS. The reason MusicBrainz asks for a User Agent is so that they contact you if your client is misbehaving. So make sure to include your contact info in the User-Agent string.
hm, according to npm, https wasn't updated in five years. So let's assume, you would use something newer like axios. Here, the request would be like this:
const callmbapi = function (mbid) {
const axios = require('axios');
return axios
.get('https://musicbrainz/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, i.e. you need to call .then(function (artistdata) { /* ... */ })
on the function (instead of using a callback).
With a more modern Node.js, you could use await instead:
const callmbapi = async function (mbid) {
const axios = require('axios');
try {
return axios.get('https://musicbrainz/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.
本文标签: javascriptNodejs HTTPget() add user agentStack Overflow
版权声明:本文标题:javascript - Nodejs HTTP.get() add user agent - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742018974a2414308.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论