Search code examples
gomicroservicesgrpcprotoc

gRPC generated file doesn't have json definition


I'm trying to generate Go file from proto file but it doesn't have json definition in the method's input definition. Should I add the json definition by myself or there were something wrong with my script. Thank you, I sincerely appreciate your help.

Proto file

message RateRequest {
    string Base = 1;
    string Destination = 2;
}

Generated file

type RateRequest struct {
    state         protoimpl.MessageState
    sizeCache     protoimpl.SizeCache
    unknownFields protoimpl.UnknownFields

    //No json definition here
    Base        string `protobuf:"bytes,1,opt,name=Base,proto3" json:"Base,omitempty"`
    Destination string `protobuf:"bytes,2,opt,name=Destination,proto3" json:"Destination,omitempty"`
}

Protoc script

protoc --go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
protos/currency.proto

grpcurl

grpcurl --plaintext -d '{Base: "GBP", Destination: "USD"}' localhost:9092 Currency.GetRate
// Error invoking method "Currency.GetRate": error getting request data: message type RateRequest has no known field named base

Solution

  • Since the error is

    error getting request data: message type RateRequest has no known field named base

    And the json is

    json:"Base,omitempty"`
    

    Then it seems it's looking for the wrong field, it should be

    json:"base,omitempty"`