apigoprotocol-buffersgrpcproto

How do I pass a json body request to api in a map string data structure in Golang?


I am new to golang and grpc, need guidance and clarification. I have below definition as a parameter to call a POST request for an external API.

    params := map[string]string{
    "movie":       movie,
    "seat":         seat,
    "pax": fmt.Sprint(pax),
    "class":      class,
}

In proto file, I have below:

message TicketData {
    string movie= 1;
    string seat= 2;
    uint32 pax= 3;
    string class = 4;
}

message SearchMovieRequest {
    TicketData data= 1;
}

However in POSTMAN (grpc request), the body request is showing below:

{
    "data": 
        {
            "movie": "abc",
            "seat": "123",
            "pax": 2,
            "class ": "b""
        }
   
}

the request body should be below:

{
    "data": **[**
        {
            "movie": "abc",
            "seat": "123",
            "pax": 2,
            "class ": "b""
        }
    **]** - missing brackets in my json body
}

I have tried using structpb and also map string interface. It doesn't seem to work. Any pointer will be appreciated. Thank you.


Solution

  • You want the data field to be repeated TicketData.

    See e.g. Specifying Field Rules in the Protobuf Language Guide (proto3).

    Specifically:

    message TicketData {
        string movie= 1;
        string seat= 2;
        uint32 pax= 3;
        string class = 4;
    }
    
    message SearchMovieRequest {
        repeated TicketData data= 1;
    }
    

    NOTE Although you include protobuf definitions, your examples are JSON. Protobuf implementations usually include automatic mappings between protobuf and JSON which is -- I assume -- what you're presenting.