【问题标题】:GRPC: Define proto post endpoint accepts both body and request paramsGRPC:定义 proto post 端点同时接受正文和请求参数
【发布时间】:2019-12-04 08:15:46
【问题描述】:

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

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

知道如何让它工作吗?

【问题讨论】:

    标签: grpc proto


    【解决方案1】:

    我自己发现的。方法是:

    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

    【讨论】:

      猜你喜欢
      • 2020-12-01
      • 1970-01-01
      • 2018-11-13
      • 1970-01-01
      • 1970-01-01
      • 2019-07-18
      • 2021-11-14
      • 1970-01-01
      • 2023-04-10
      相关资源
      最近更新 更多