Killer Beast
Killer Beast

Reputation: 509

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?

Upvotes: 4

Views: 4201

Answers (2)

subhajit das
subhajit das

Reputation: 445

Hey @killer Beast your ans is right but we have to provide the correct name of the field . In this case I received an error called " no field "testbody" found in TestRequest " . we have to replace the testbody to body .

post :"testbody" -> "body"

Upvotes: 1

Killer Beast
Killer Beast

Reputation: 509

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

Upvotes: 5

Related Questions