【问题标题】:Node JS Call GRPC Server with custom headersNode JS 使用自定义标头调用 GRPC 服务器
【发布时间】:2016-12-10 19:39:22
【问题描述】:

我正在尝试调用 GRPC 端点,但我想提供客户身份验证标头。我在哪里指定这个?

var client = new proto.Publisher('127.0.0.1:50051',
    grpc.credentials.createInsecure());

var customHeader = { 
    'authorization': 'secret'
}

client.publish(data, function (err, response) {
  console.log('Sent');
});

【问题讨论】:

    标签: node.js grpc


    【解决方案1】:

    您需要创建一个grpc.Metadata 对象,然后将其作为可选参数传递给方法:

    var client = new proto.Publisher('127.0.0.1:50051',
        grpc.credentials.createInsecure());
    
    var metadata = new grpc.Metadata();
    metadata.add('authorization', 'secret')
    
    client.publish(data, metadata, function (err, response) {
      console.log('Sent');
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-10
      • 2016-02-26
      • 1970-01-01
      • 2017-11-09
      • 1970-01-01
      • 2020-05-21
      • 1970-01-01
      相关资源
      最近更新 更多