ojaoweir
ojaoweir

Reputation: 55

Nodejs HTTP.get() add user agent

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

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.

Upvotes: 5

Views: 4831

Answers (2)

warp
warp

Reputation: 1578

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.org/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.

Upvotes: 6

Ryuno-Ki
Ryuno-Ki

Reputation: 800

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

Upvotes: 1

Related Questions