Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node JS Call GRPC Server with custom headers

Tags:

node.js

grpc

I am trying to call a GRPC endpoint but I want to provide a customer auth header. Where do I specify this?

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');
});
like image 469
George Harnwell Avatar asked Aug 04 '16 15:08

George Harnwell


People also ask

Do gRPC calls have headers?

Headers passed in gRPC communication are categorized into two types: call-definition headers and custom metadata. Call-definition headers are predefined headers supported by HTTP/2. Those headers should be sent before custom metadata.

How to call gRPC from JavaScript?

Your scripts should be like this: Now, to run the code open your terminal and run npm run grpc-server to start your server. Then open another terminal and run npm run grpc-client and you would get the results on your terminal.

Does JavaScript support gRPC?

Building gRPC Web clients In the case of gRPC-web client, an npm package is generated and then published to the GitHub package repository. Then, JavaScript applications can consume this package using the npm install command.


1 Answers

You need to create a grpc.Metadata object, then pass it as an optional argument to the method:

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');
});
like image 81
murgatroid99 Avatar answered Sep 29 '22 21:09

murgatroid99