简体   繁体   English

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

[英]How to Transcode HTTP DELETE with Request Body to gRPC

I'm new in using gRPC with proto3, and I've used Transcoding HTTP/JSON to gRPC to migrate existing http endpoints to grpc.我是在 proto3 中使用 gRPC 的新手,并且我已经使用将Transcoding HTTP/JSON to gRPC来将现有的 http 端点迁移到 grpc。

But I have http DELETE request with request body.但是我有带有请求正文的 http DELETE 请求。 I have tried following and got an error.我试过跟随并出现错误。

Grpc Endpoint : Grpc 端点:

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

protoc gererate command as below protoc gererate 命令如下

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

Error I have Got我有错误

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

And then I have add --allow_delete_body=true to my protoc command as below.然后我将--allow_delete_body=true添加到我的--allow_delete_body=true命令中,如下所示。

--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

grpc versions in my go.mod我的 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

Would anyone kindly explain How I transcode HTTP DELETE to grpc with request body.任何人都可以解释一下我如何使用请求正文将 HTTP DELETE 转码为 grpc。

The protoc always has wired issues. protoc 总是有有线问题。 Suggest use buf instead.建议使用buf代替。 And the config of buf.gen.yaml should like this and it works well:而 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

For this question, I found answer in Not able to pass allow_delete_body to protoc-gen-grpc-gateway , this command works for me, but not work after add --openapiv2_out=./openapi :对于这个问题,我在无法将 allow_delete_body 传递给 protoc-gen-grpc-gateway 中找到答案,此命令对我--openapiv2_out=./openapi ,但在添加--openapiv2_out=./openapi

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

After spent time on web and trying with applying flags in different ways, I have found the working command for me.在网上花时间并尝试以不同方式应用标志后,我找到了适合我的工作命令。 Thanks everyone help to resolve this.谢谢大家帮忙解决这个问题。

This is the working command for me:这是我的工作命令:

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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM