简体   繁体   English

如何将python gRPC服务的尾随元数据发送到grpc-web客户端?

[英]How to send trailing metadata from python gRPC service to grpc-web client?

I'm trying to send trailing metadata from python gRPC service to grpc-web client. 我正在尝试将python gRPC服务的尾随元数据发送到grpc-web客户端。 But on client side I cannot receive it. 但在客户端我无法收到它。

I'm using envoy proxy server to connect grpc-web with pyhton gRPC service. 我正在使用envoy代理服务器将grpc-web与pyhton gRPC服务连接起来。

For sending metadata I'm using this method: 为了发送元数据我正在使用这种方法:

class Greeter(helloworld_pb2_grpc.GreeterServicer):
    def SayHello(self, request, context):
        context.set_trailing_metadata((
            ('checksum', 'I agree'),
            ('retry', 'false'),
        ))
        return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name)

On client I'm trying to access metadata this way: 在客户端我试图以这种方式访问​​元数据:

var call = greeterService.sayHello(request, {}, function(err, response) {
  if (err) {
    console.log(err.code);
    console.log(err.message);
  } else {
    console.log(response.getMessage());
  }
});
call.on('status', function(status) {
  console.log(status.code);
  console.log(status.details);
  console.log(status.metadata); // here should arrive metadata
});

I'm sending it how it is described in documentation. 我发送它如何在文档中描述。 But metadata is not arrived. 但元数据尚未到达。

Did anybody have such problem? 有没有人有这样的问题?

Before calling method set_trailing_metadata , you need to call send_initial_metadata with same medatdata keys but any values. 在调用方法set_trailing_metadata之前,需要使用相同的medatdata键调用send_initial_metadata ,但需要调用任何值。

In my case it will looks like this: 在我的情况下,它将如下所示:

class Greeter(helloworld_pb2_grpc.GreeterServicer):
    def SayHello(self, request, context):
        context.send_initial_metadata((
            ('checksum', ''),
            ('retry', ''),
        ))
        context.set_trailing_metadata((
            ('checksum', 'I agree'),
            ('retry', 'false'),
        ))
        return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name)

Actually I found this solution accidentally. 其实我偶然发现了这个解决方案。

I assume that you need to specify headers names in initial metadata and only then you should set headers values in trailing metadata and then envoy could pack it properly and deliver via HTTP 1.1. 我假设你需要在初始元数据中指定标题名称 ,然后你应该在尾随元数据中设置标题值 ,然后envoy可以正确打包并通过HTTP 1.1传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM