grpcproto

GRPC: Define proto post endpoint accepts both body and request params


Being new to GRPC, I am struggling to figure out how to define an endpoint in a proto where it accepts both body and the request params. The request params are optional and not necessary in every request. The following is my code as of now:

rpc TestService (TestRequest) returns (TestResponse) {
    option (google.api.http) = {
        post: "/api/v1/test"
        body: "*"
    };
}

In my TestRequest definition, I have:

message TestRequest {
    google.protobuf.StringValue param_1 = 1;
    google.protobuf.StringValue param_2 = 2;
    google.protobuf.StringValue body_1 = 3;
    google.protobuf.StringValue body_2 = 4;
}

My curl command would look something like:

curl -X POST 'http://localhost/api/v1/test?param_1=data_param_1&param_2=data_param_2' -d '{
    "body_1" : "data_body_1",
    "body_2" : "data_body_2"
}'

Any idea how to get it working?


Solution

  • Found it out myself. The way to do it is:

    rpc TestService (TestRequest) returns (TestResponse) {
        option (google.api.http) = {
            post: "/api/v1/test"
            body: "testbody"
        };
    }
    

    And then:

    message TestRequest {
        google.protobuf.StringValue param_1 = 1;
        google.protobuf.StringValue param_2 = 2;
        TestBody body = 3;
    }
    

    And also:

    message TestBody {
        google.protobuf.StringValue body_1 = 1;
        google.protobuf.StringValue body_2 = 2;
    }
    

    Reference Links:

    https://github.com/grpc-ecosystem/grpc-gateway/issues/234

    https://cloud.google.com/endpoints/docs/grpc/transcoding