1

I'm blocked since 3 days and did my research on the internet. Here' is the code.

api.js

const express = require('express');
const router = express.Router();
var http = require('http');
var https = require("https");

router.get('/github', async function (req, res) {
    https.get('https://api.github.com/v3/users/tmtanzeel', function (apiRes) {
        apiRes.pipe(res);

    }).on('error', (e) => {
        console.error(e);
        res.status(500).send('Something went wrong');
    });
});

Output:

Request forbidden by administrative rules. Please make sure your request has a User-Agent header (http://developer.github.com/v3/#user-agent-required). Check https://developer.github.com for other possible causes.

I found something useful here:

  1. In Node.js/Express, how do I automatically add this header to every "render" response?
  2. Requesting https://api.github.com/users (api) returns System.Net.HttpStatusCode.Forbidden IsSuccessStatusCode = false

But these were not very helpful.

I tried: res.setHeader("User-Agent", "My App"); but I'm not very sure about the second argument.

Modified server.js

const express = require('express');
const app = express();
const api = require('./routes/api');
const cors = require('cors');
app.use(cors());

app.use(function (req, res, next) {

    // Website you wish to allow to connect
    res.setHeader('Access-Control-Allow-Origin', '*');

    // Request methods you wish to allow
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');

    // Request headers you wish to allow
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');

    // Set to true if you need the website to include cookies in the requests sent
    // to the API (e.g. in case you use sessions)
    res.setHeader('Access-Control-Allow-Credentials', true);

    res.setHeader("User-Agent", "My App"); // <-------------ADDED HEADER HERE

    // Pass to next layer of middleware
    next();
});

app.use('/api', api);
app.get('/', function (req, res) {
    res.send('Server is up and running!');
})

app.listen(3000, function () {
    console.log('Server listening on 3000 port');
});

Have you ever face this kind of issue. Please help.

Tanzeel
  • 4,174
  • 13
  • 57
  • 110

1 Answers1

1

You are setting headers to your response. Instead, you must set headers in the API call you make. You can pass options object to the http.get() method and set headers there.

router.get('/github', async function (req, res) {

    const options = {
       hostname: 'api.github.com',
       path: '/v3/users/tmtanzeel',
       headers: {
           'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1521.3 Safari/537.36'
       }
    }

    https.get(options, function (apiRes) {
        apiRes.pipe(res);

    }).on('error', (e) => {
        console.error(e);
        res.status(500).send('Something went wrong');
    });
});

See this answer on setting github user-agent header:

https://stackoverflow.com/a/16954033/4185234

Charlie
  • 22,886
  • 11
  • 59
  • 90
  • 1
    I was reading https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent – Tanzeel Oct 01 '21 at 17:30
  • Now I'm getting `{ "message": "Not Found", "documentation_url": "https://docs.github.com/rest" } ` However I've added `const options = { hostname: 'api.github.com', path: '/v3/users/tmtanzeel', headers: { 'User-Agent': ..., OAuth: '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' }` – Tanzeel Oct 01 '21 at 17:36
  • 1
    I think I'll do some more document reading at : https://docs.github.com/rest – Tanzeel Oct 01 '21 at 17:39
  • 1
    Since you've answered the actual question I'm accepting this answer. But please look into my other issue (that i mentioned above) if you've time. I need expert help real bad. – Tanzeel Oct 01 '21 at 17:40