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.

16
  • try hitting without "User-Agent": "request" and json:true Commented Sep 7, 2018 at 9:04
  • @mehta-rohan The result was this statusCode: 403 body: Request forbidden by administrative rules. Please make sure your request has a User-Agent header (developer.github.com/v3/#user-agent-required). Check developer.github.com for other possible causes. Commented Sep 7, 2018 at 9:35
  • tried adding user-agent and json:true. Still total results are 0 Commented Sep 7, 2018 at 9:36
  • remove just json:true Commented Sep 7, 2018 at 9:37
  • works or not..? Commented Sep 7, 2018 at 9:45

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.