Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

156
Views
How to make a GET call to api.github with Express server

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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!