最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Nodejs HTTP.get() add user agent - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 6

With 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.

发布评论

评论列表(0)

  1. 暂无评论