http - gRPC-Gateway:有没有办法以 JSON 数组的形式返回响应?

标签 http protocol-buffers grpc grpc-gateway

我正在实现 https://todobackend.com/使用 gRPC 网关。 https://todobackend.com/的规范要求一些响应采用 JSON 数组的形式,例如:

GET /todos
=> [{ "title": "...", ... }, { ... }]

但是据我所知,通过使用 gRPC-Gateway 我必须返回对象,例如 { "todos": [{ ... }, { ... }] }。我可以返回数组而不是对象吗?

最佳答案

我找到了this thread并使其与 response_body 选项以及 allow_repeated_fields_in_body CLI 参数一起使用。

  rpc Add(Todo) returns (Todo) {
    option (google.api.http) = {
      post: "/v1/todos",
      body: "*"
    };
  };
protoc -I proto/ -I googleapis \
  --go_out ./proto --go_opt paths=source_relative \
  --go-grpc_out ./proto --go-grpc_opt paths=source_relative \
  --grpc-gateway_out=allow_repeated_fields_in_body=true:./proto --grpc-gateway_opt paths=source_relative \
  ./proto/todo/todo.proto
# note "allow_repeated_fields_in_body=true"

关于http - gRPC-Gateway:有没有办法以 JSON 数组的形式返回响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66407366/

相关文章:

ios - 从 ios 设备连接到 localhost

android - Blogger api 总是返回 401 错误响应

apache - 如何理解 "semantically transparent"部分RFC2616的 "Cache-Control machanism"?

python - 如何从 protobuf 生成 python 类文件

java - 使用依赖注入(inject)从另一个 GrpcService 调用一个 GrpcService

http - Angular2 http.get()、map()、subscribe() 和可观察模式——基本理解

.net - protobuf-net 的替代方案 - 使用对象图的大小和时间高效的序列化程序

protocol-buffers - Protocol Buffer : define a parentS <-> childrenS relationship

python - 使用 Session.run 在 Tensorflow 代码中进行并行编程

python - 我无法安装库 grpcio