如何将带有请求正文的 HTTP DELETE 转码为 gRPC

问题描述

我是在 proto3 中使用 gRPC 的新手,我已经使用 Transcoding HTTP/JSON to gRPC 将现有的 http 端点迁移到 grpc。

但是我有带有请求正文的 http DELETE 请求。我尝试了以下操作,但出现错误

Grpc 端点:

  rpc DeleteFile(DeleteFileRequest) returns (DeleteFileResponse) {
    option (google.api.http) = {
      delete: "/v2/file/delete/{path}"
      body: "*"
    };
  }

protoc gererate 命令如下

protoc -I ./proto --go-grpc_out=. --go_out=. --grpc-gateway_out=. --openapiv2_out=./openapi ./proto/myapp.proto

我遇到的错误

--grpc-gateway_out: must not set request body when http method is DELETE except allow_delete_body option is true

然后我将 --allow_delete_body=true 添加到我的 protoc 命令中,如下所示。

--allow_delete_body=true
error : UnkNown flag: --allow_delete_body

--grpc-gateway_opt allow_delete_body=true 
error : must not set request body when http method is DELETE except allow_delete_body option is true

我的 go.mod 中的 grpc 版本

github.com/grpc-ecosystem/grpc-gateway/v2 v2.3.0
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
google.golang.org/genproto v0.0.0-20210224155714-063164c882e6
google.golang.org/grpc v1.36.0
google.golang.org/protobuf v1.26.0

有人能解释一下我如何使用请求正文将 HTTP DELETE 转码为 grpc。

解决方法

protoc 总是有连线问题。建议改用 buf。而 buf.gen.yaml 的配置应该是这样的,它运行良好:

version: v1beta1
plugins:
  - name: go
    out: proto
    opt: paths=source_relative
  - name: go-grpc
    out: proto
    opt: paths=source_relative,require_unimplemented_servers=false
  - name: grpc-gateway
    out: proto
    opt:
      - paths=source_relative
      - allow_delete_body=true

对于这个问题,我在 Not able to pass allow_delete_body to protoc-gen-grpc-gateway 中找到了答案,此命令对我有用,但在添加 --openapiv2_out=./openapi 后不起作用:

protoc -I ./proto --go-grpc_out=. --go_out=. --grpc-gateway_out=allow_delete_body=true:. ./proto/myapp.proto
,

在网上花时间并尝试以不同方式应用标志后,我找到了适合我的工作命令。谢谢大家帮忙解决这个问题。

这是我的工作命令:

protoc -I ./proto --go-grpc_out=. --go_out=. --grpc-gateway_out=allow_delete_body=true:. --openapiv2_opt allow_delete_body=true --openapiv2_out=./openapi ./proto/myapp.proto