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:
- In Node.js/Express, how do I automatically add this header to every "render" response?
- 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.
CodePudding user response:
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: