[英]For Transcoding HTTP/JSON to gRPC, how to specify query parameter for enum
I have setup http-grpc transcoding for my service (see https://cloud.google.com/endpoints/docs/grpc/transcoding ).我已经为我的服务设置了 http-grpc 转码(请参阅https://cloud.google.com/endpoints/docs/grpc/transcoding )。 It has an rpc DoSomething
defined as follows:它有一个 rpc DoSomething
定义如下:
service MyService {
// Determine experiments and treatments for a user across multiple namespaces.
rpc DoSomething (RpcRequest) returns (RpcResponse) {
option (google.api.http) = {
get: "/my-service/do-something"
};
};
}
message RpcRequest {
// Metadata associated with this request.
RequestContext request_context = 1;
}
message RequestContext {
// DeviceID for identifying user.
string device_id = 1;
// Just added this, but don't know how to pass in this value as query parameter!
Country country = 2;
}
// Just added this
enum Country {
UNKNOWN_COUNTRY = 0;
US = 1;
CA = 2;
AF = 3;
AX = 4;
// ... etc.
}
When I first implemented this, RequestContext
only had device_id, so this REST URL worked fine: GET https://example.com/my-service/do-something?request_context.device_id=abc123
.当我第一次实现这个时, RequestContext
只有 device_id,所以这个 REST URL 工作正常: GET https://example.com/my-service/do-something?request_context.device_id=abc123
。 It executed the operation and returned the correct response as expected.它执行了操作并按预期返回了正确的响应。
Now I need to add an enum country
to RequestContext
, but I'm not sure how to pass in that information in the query string of the URL.现在我需要向RequestContext
添加一个枚举country
,但我不确定如何在 URL 的查询字符串中传递该信息。 I've tried GET https://example.com/my-service/do-something?request_context.device_id=abc123&request_context.country=US
, but that does not forward the country onto the GRPC service;我试过GET https://example.com/my-service/do-something?request_context.device_id=abc123&request_context.country=US
,但这并没有将国家转发到 GRPC 服务; it just assigns the default value 0, which is "UNKNOWN_COUNTRY", to country
.它只是将默认值 0 分配给country
,即“UNKNOWN_COUNTRY”。 I have not found any guidance on Google's documentation about this.我还没有找到有关 Google 文档的任何指导。
What is the correct way to specify an enum in the query parameter?在查询参数中指定枚举的正确方法是什么? Thanks in advance!提前致谢!
It should work passing enum value as the string defined in the proto file regardless in the query parameters or in the json body.无论是在查询参数中还是在 json 正文中,它都应该将枚举值作为 proto 文件中定义的字符串进行传递。 You can also pass enum value as integer too.您也可以将枚举值作为整数传递。
You may have some typo and trailing space that causing the conversion code not finding the match value.您可能有一些拼写错误和尾随空格,导致转换代码找不到匹配值。 If mis-matched, enum value default to 0.如果不匹配,枚举值默认为 0。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.