1

I am trying to fetch the results from all repositories of my organisation under a search category. The results are fetched properly when I use the curl command as shown below

curl -H "Authorization: token ****" -i https://api.github.com/search/code?q=org:<org>+<search_param>

But when I try to run it programmatically in nodejs via the request module it is not returning any results. My code is as shown below

const request = require("request");
const options = {
    url:'https://api.github.com/search/code?q=org:<org>+<search_param>'
    headers: {
        "Autorization": "token ***",
        "User-Agent": "request"
    },
    json:true
}
console.log(options);
request.get(options, function (error, response, body) {
    console.log('error:', error); // Print the error if one occurred
    console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
    console.log('body:', body); // Print the HTML for the Google homepage.
});

The output for the above code is as below

body: {"total_count":0,"incomplete_results":false,"items":[]}

Please let me know what is wrong with the above code or if I am missing anything.

amit1310
  • 8,825
  • 2
  • 12
  • 12

1 Answers1

1

I was able to solve this by using the axios module instead of request module as the request module does not send the Authorization hader. Got a reference from Nodejs request module doesn't send Authorization header.

The updated code which works is as follows

const axios = require("axios");
const options = {
    method:"get",
    url:'https://api.github.com/search/code?q=org:<org>+<searchtoken>',
    headers: {
        "Authorization": "token ***",
        "User-Agent": "abc"
    }
}
console.log(options);
axios(options).then(function ( response) {
    console.log('statusCode:', response); // Print the response status code if a response was received
    // console.log('body:', body); // Print the HTML for the Google homepage.
}).catch(function (error) {
    console.log(error);
});

Thanks @mehta-rohan for the help

amit1310
  • 8,825
  • 2
  • 12
  • 12