wooseop
wooseop

Reputation: 337

gRPC-Gateway: Is there a way to return responses in form of JSON arrays?

I'm making an implementation of https://todobackend.com/ using gRPC-Gateway. https://todobackend.com/'s spec requires some responses to be in form of JSON arrays, like:

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

But AFAIK by using gRPC-Gateway I must return objects, like { "todos": [{ ... }, { ... }] }. Can I return arrays instead of objects?

Upvotes: 2

Views: 2038

Answers (1)

wooseop
wooseop

Reputation: 337

I found this thread and got it working with response_body option along with allow_repeated_fields_in_body CLI argument.

  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"

Upvotes: 2

Related Questions