node.js - 在 Node.js 中使用 REST API 时请求参数

标签 node.js rest request

我想使用 request.js 在 Node.js 中使用 REST 服务,如下所示:

var request = require('request');
request.get({
  url: 'https://www.googleapis.com/storage/v1/b',
  auth: {
    'bearer': 'oauth2_token'
  }
}, function(err, res) {
  console.log(res.body);
});

但是,我还想指定一组请求参数,例如 projectprefix 等(如 https://cloud.google.com/storage/docs/json_api/v1/buckets/list 中指定)。

如何在消费API服务的请求中传递这些参数?

最佳答案

您可以传入 qs 作为附加查询。请参阅下面的示例:

 const queryObject = { project: 'project', prefix: 'prefix' };

 request.get({
    url: 'https://www.googleapis.com/storage/v1/b',
    qs: queryObject,
    auth: {
      'bearer': "oauth2_token"
    }
  }, function(err, res) {
    console.log(res.body);
  });

参见here for github issue .

关于node.js - 在 Node.js 中使用 REST API 时请求参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48972935/

相关文章:

javascript - 如何从文件中读取数据而不对精度超过 16 位的数字进行舍入?

javascript - 如何将输入给出的文本存储到 JSON 文件

php - MySQL/PHP 忽略现有用户(有时)

node.js - 如何仅在稍后将值导出到其他地方后才在 Node js 中导入值?

javascript - Passport 单选按钮重定向到注册页面

node.js - NodeJS:发现错误:听 EADDRINUSE:::3000?

json - 如何测试期望通过 POST 请求接收 JSON 数组的 Controller 操作?

python - 发送请求时出现证书错误

rest - BOX API :how to get location attribute in response using https://api. box.com/2.0/files/{fileId}/content 下载

node.js - 在后端有条件地更新/删除文档。我是不是该?我是否分配 PUT、POST 或 DELETE http 方法?