Skip to content Skip to sidebar Skip to footer

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('htt

Solution 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', asyncfunction (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

Post a Comment for "How To Make A Get Call To Api.github With Express Server"