4

I'm using grpcc, which is based on protobuf.js, to test my grpc service APIs.

My .proto file:

message MyRequest {
    string userId = 1;
    map<string, string> params = 2;
}

I tried the following json body to send a request:

{userId : "my_user_id" , params: { name: "my_name"}}
{userId : "my_user_id" , params: [{"name":"my_name"}] }

But this gives the following error:

Error: Illegal value for Message.Field....MyRequest.params of type string: object (expected ProtoBuf.Map or raw object for map field)

How to correctly represent a protobuf map as a json?

Maddy
  • 2,114
  • 7
  • 30
  • 50

1 Answers1

3

The proper json body would be the following:

{ "userId": "my_user_id", "params": { "name": "my_name" } }

What you've tried doing is an array of maps, which doesn't really mean anything in the context of protobuf. A map<string, string> is exactly the description of a json object, so more than one value would be represented the following way:

{ "params": { "key1": "value1", "key2": "value2" } }

No need for an array.

Nicolas Noble
  • 699
  • 3
  • 7
  • 1
    I already tried this way. still the same error. I added the both cases I tried – Maddy Apr 27 '18 at 08:21
  • 1
    Maybe this is tied to grpcc then. Protobuf maps really do translate into plain objects in JavaScript when used with protobufjs and grpc. I've been using this property in my grpc project for a while now. – Nicolas Noble Apr 27 '18 at 15:29