我的 Java 服务中有一个 POST 端点,需要 protobuf 3 负载。该服务由发送 protobuf 有效负载的其他服务使用。我想进行一些调试并通过
curl
命令将 protobuf 有效负载发送到服务,但是,我不知道如何构建这样的有效负载,因为它是二进制协议。
您可以使用 protoCURL 来实现此目的。它是一个命令行工具,用于发送和接收 protobuf HTTP REST 请求,同时以 Protobuf Text 或 JSON 格式编写请求。
请求可能看起来像这样:
protocurl -I path/to/protos -i ..MyRequest -o ..MyResponse \
-u http://host/path/o/endpoint -d 'someField: "some-string", someIntField: 42'
响应可能如下所示:
=========================== Request Text =========================== >>>
someField: "my-string"
someIntField: 42
=========================== Response Text =========================== <<<
someResponseField: "The answer for everything!"
someOtherResonseMessage: {
fooEnum: BAR
}
您可以在存储库中找到有关如何使用它的更多示例。
(免责声明:我是这个工具的作者并创建了它,因为我遇到了同样的问题并且需要更好的工具。)
我也会使用 protocurl,但如果您有一个已编码的文件并且想要使用curl 进行 HTTP POST 请求,您可以将其运行为:
curl -X POST -H "Content-Type: application/x-protobuf" --data-binary @encodedFileName http://localhost:8080/or-any-other-url-path