Sending Struct from browser (grpc-web) server (python grpc) results in empty Struct

28 views
Skip to first unread message

cyb...@gmail.com

unread,
Jul 3, 2020, 5:12:31 PM7/3/20
to grpc.io
I've been struggling with passing a JSON with Message values back and forth between a browser (grpc-web) and server (python grpc). A Struct seems to be the way but even though the struct I'm sending looks perfectly fine, it is empty once received by the server.

```
// proto
message Result {
    google.protobuf.Struct variables = 1;
}

// obj - Where variables would contain a (1 level deep) JSON with different types of values, e.g.:
{
    "key1": 1,
    "key2": true,
    "key3": proto_msg_a //instance of proto.MessageA
}

// code (js in browser using grpc-web)
struct = new proto.google.protobuf.Struct(obj);
req = new Request;
req.variables = struct;

```

Checking `req.variables` before sending shows that it's indeed a `Struct` with all the correct fields in it. But once the other end (server) receives it `req.variables` is an empty `Struct`. For testing purposes I tried an `obj` that is simply `{'key': 'value'}`, but the result was the same.


I would really like to use proto messages in variables. The reason for picking protobuf/grpc was exactly this, being able to use the same type throughout our complete platform, but this seems to be blocking this goal. Did I miss something? What would you do?

cyb...@gmail.com

unread,
Jul 3, 2020, 5:40:54 PM7/3/20
to grpc.io
I just found out about `proto.google.protobuf.Struct.fromJavaScript` and tried it:

```
// code
struct = proto.google.protobuf.Struct.fromJavaScript(vars);
req = new Request;
req.variables = struct;
```

This works for a simple `obj` (e.g. `{"key": "val"}`), but for an `obj` with a proto message field (such as above) it resulted in :

```
struct_pb.js:875 Uncaught Error: Unexpected struct type.
    at Function.proto.google.protobuf.Value.fromJavaScript (struct_pb.js:875)
    at Function.proto.google.protobuf.Struct.fromJavaScript (struct_pb.js:941)
    at Function.proto.google.protobuf.Value.fromJavaScript (struct_pb.js:871)
    at Function.proto.google.protobuf.Struct.fromJavaScript (struct_pb.js:941)
    at Function.proto.google.protobuf.Value.fromJavaScript (struct_pb.js:871)
    at Function.proto.google.protobuf.Struct.fromJavaScript (struct_pb.js:941)
    at Function.proto.google.protobuf.Value.fromJavaScript (struct_pb.js:871)
    at Function.proto.google.protobuf.Struct.fromJavaScript (struct_pb.js:941)
```

cyb...@gmail.com

unread,
Jul 4, 2020, 6:00:17 AM7/4/20
to grpc.io
Or can I, instead of going through all the troubles with protobuf/json in javascript, just use a `map`?
```
// proto
message Request {
    map<string, ?type?> variables = 1;
}
```
But what  would `?typ?` then be if it the values can ben anything (proto.MessageX, string, boolean, etc)?

cyb...@gmail.com

unread,
Jul 4, 2020, 6:47:40 AM7/4/20
to grpc.io
Oh, btw, one advantage is that the receiving end knows all the proto definitions it might receive.

Stanley Cheung

unread,
Jul 6, 2020, 2:12:43 AM7/6/20
to cyb...@gmail.com, grpc.io
Looks like this is a protobuf question rather than a grpc-web question. Looks like proto.google.protobuf.Struct.fromJavaScript is a proto3 API? You might need to ask the question to https://github.com/protocolbuffers/protobuf.

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/35b07876-e6d5-4dd3-ae7d-0608fc74949ao%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages