node.js - 从nodejs中的请求模块调用时,github搜索api没有给出结果

标签 node.js curl github-api

我正在尝试从我的组织的搜索类别下的所有存储库中获取结果。当我使用curl命令时,结果被正确获取,如下所示

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

但是当我尝试通过请求模块在 NodeJS 中以编程方式运行它时,它没有返回任何结果。 我的代码如下所示

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.
});

上述代码的输出如下

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

请告诉我上面的代码有什么问题或者我是否遗漏了任何内容。

最佳答案

我能够通过使用 axios 模块而不是请求模块来解决这个问题,因为请求模块不发送授权哈德尔。得到了来自Nodejs request module doesn't send Authorization header的引用.

更新后的代码如下

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);
});

感谢@mehta-rohan 的帮助

关于node.js - 从nodejs中的请求模块调用时,github搜索api没有给出结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52218517/

相关文章:

node.js - 安装 Node 包而不重新启动 docker compose

node.js - Cron 作业在 firebase 函数中不起作用

python - 使用curl将文件上传到django服务器

http - CURL 的实时 HTTP

javascript - 我可以在开发 firefox Os 应用程序时使用 indexedDB 吗

javascript - 为什么我不能用 Mongoose 设置数据库?

node.js - GAE 上的 Nodejs Websocket

php - 无需下载页面即可批量检查http状态代码

ruby - 我可以通过 GH API 获取两次提交之间一个文件的差异数据吗?

javascript - 使用 Fetch 返回 GitHub 用户数据