4

I have a service defined this way :

service Service {
    rpc SearchCategory(SearchCategoryRequest) returns (SearchCategoryResponse) {
        option (google.api.http) = {
            get: "/v1/categories/search"
        };
    }
    rpc GetCategory(GetCategoryRequest) returns (GetCategoryResponse) {
        option (google.api.http) = {
            get: "/v1/categories/{id.val}"
        };
    }
}

The problem is that even if I call search?q=MyQuery, it is caught by the GetCategory method and it tries to get the category with id search.

I suppose it is because the paths are very close. Is there a way of defining a priority in the routes like one would do in a classic web application ?

Thanks

c4k
  • 4,270
  • 4
  • 40
  • 65

2 Answers2

1

It's been a while since this question but I recently just ran into the exact same issue myself and found out the solution.

So basically, the lower you put your endpoint in your protobuf service definition, the higher it's priority.

For your case, you need to swap two endpoints so that SearchCategory is under GetGategory, which gives it higher priority.

service Service {
    rpc GetCategory(GetCategoryRequest) returns (GetCategoryResponse) {
        option (google.api.http) = {
            get: "/v1/categories/{id.val}"
        };
    }
    rpc SearchCategory(SearchCategoryRequest) returns (SearchCategoryResponse) {
        option (google.api.http) = {
            get: "/v1/categories/search"
        };
    }
}

then /v1/categories/search should work.

Miigon
  • 779
  • 6
  • 18
-1

Not use {id.val}, use {id}. And will be work

message GetCategory {
    string id = 1;
}