简体   繁体   English

将 HTTP/JSON 转码为 gRPC,如何为 enum 指定查询参数

[英]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.

相关问题 如何指定枚举对象数组的RESTful资源查询参数 - How to specify RESTful resource query parameter of an array of enum objects 如何在HTTP请求中将JSON作为查询参数传递? - How to pass JSON as a query parameter in a HTTP request? 如何在HTTP GET请求中指定参数 - How to specify a parameter in an HTTP GET request 如何在json-ld中为资源指定受支持的http操作? - How to specify supported http operation for a resource in json-ld? 如何在 GET 请求查询参数中读取 JSON - How to read JSON in GET request query parameter 如何在Java中使用查询参数更改HTTP方法? - How to change the HTTP method by using query parameter in Java? 查询参数保留为 json - query parameter preserve as json Twilio nodejs客户端指定PageSize查询参数 - Twilio nodejs client specify PageSize query parameter 如何将 json url 查询中的 esriFieldTypeDate 参数格式化为 ESRI REST 服务器 - How to format a esriFieldTypeDate parameter in a json url query to an ESRI REST server 如何在 C# Rest API 中处理查询参数中的可选 ENUM - 无效和空值并隐藏 Swagger 中的特定值 - How to handle Optional ENUM in Query Parameter in C# Rest API - Invalid and null value and also hide specific value in Swagger
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM