GRPC:定义原型 post 端点接受正文和请求参数

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

作为 GRPC 的新手,我正在努力弄清楚如何在接受正文和请求参数的原型中定义端点。请求参数是可选的,并非在每个请求中都是必需的。以下是我目前的代码:

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

在我的 TestRequest 定义中,我有:

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;
}

我的 curl 命令类似于:

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"
}'

知道如何让它工作吗?

我自己发现的。方法是:

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

然后:

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

还有:

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

参考链接:

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

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