Reputation: 473
Im trying to achieve the following things in application created from scratch using nodeJs.
Below is my code
const express = require("express");
const app = express();
const request = require('request');
const fs = require("fs");
var path = require("path");
var rootDir = process.argv.length > 2 ? process.argv[2] : process.cwd();
var filePath = path.join(rootDir, "userList.txt");
const https = require('https');
app.listen(3002, function () {
console.log("Server running on port 3002...");
});
app.get("/getUserRepository", function (req, res, next) {
fs.readFile("myFilePath/myFile.txt", {encoding: "UTF8"}, function (err, userListObject) {
getDataObject(userListObject);
});
});
function getDataObject(userList) {
var userRepoData = [];
var userListArray = userList.split(",");
userListArray.forEach(function (userListObject) {
https.request("https://api.github.com/users/" + userListObject + "/repos", function (res) {
res.setEncoding('utf8');
res.on('data', function (data) {
userRepoData.push(JSON.parse(data));
});
}).end();
});
}
The challenge im facing is, when im making a separate call to get the repo of each user, im getting exception
"Request forbidden by administrative rules. Please make sure your request has a User-Agent header (http://developer.github.com/v3/#user-agent-required)."
Im not finding any example / approach as to where i can add the user-agent.
Also one more thing that i want to know is, where this is the best approach to achieve what i want.
Upvotes: 0
Views: 914
Reputation: 4394
Try this
userListArray.forEach(function (userListObject) {
var options = {
url: "https://api.github.com/users/" + userListObject + "/repos",
headers: {
'User-Agent': 'my node app'
}
};
https.request(options, function (res) {
res.setEncoding('utf8');
res.on('data', function (data) {
userRepoData.push(JSON.parse(data));
});
}).end();
});
Upvotes: 1
Reputation: 3642
As the error says you are missing a header User-Agent
. Refactor your code like this
function getDataObject(userList) {
let userRepoData = [];
let userListArray = userList.split(",");
let options = {
host: 'api.github.com',
path: '/users/' + username + '/repos',
method: 'GET',
headers: {'user-agent': 'node.js'}
};
userListArray.forEach(function (userListObject) {
https.request(options, function (res) {
res.setEncoding('utf8');
res.on('data', function (data) {
userRepoData.push(JSON.parse(data));
});
}).end();
});
}
You can read more about User-Agent here.
Basically you need to pass how are you trying to access github api
. If you try with browser it automatically sends the user-agent
, similar with postman
but once writing a script you need to manually pass the user-agent
Upvotes: 0