【问题标题】:how to convert protobuf readable request to proto message如何将 protobuf 可读请求转换为 proto 消息
【发布时间】:2021-11-06 22:00:42
【问题描述】:

出于教育目的,我正在使用 google api 来学习 protobuf 连接。但是当我发送帖子请求时,我收到了400 Bad Request。我希望我的 protobuf 消息有问题,并且不明白如何在 proto 文件中编写 0: 0 消息。我正在使用PHP Protobuf 库来序列化数据。

这是可读/解码的 grpc 发布请求数据:

0: 0
0: 0
}
1: 0x80c2:0x3a2f
2: this is test
3: 5
7 {
  9: nG86YfStD4Of9QPB_L_QDg:28441478
  15: 20089
}
8 {
  1 {
    1: 5
  }
}
9 {
  1: 0
  2: 1
}
11: 1
12: 1
14: 0
16 {

我按照上面的 grpc 请求数据创建了这个 proto 消息:

syntax = "proto3";

enum firstZero {
    COUNT = 0;
}

message submit {
    string pid = 1;
    string rtext = 2;
    int32 str = 3;
    .unknown_auto_data unknown = 7;
    .unknown_auto_data_eight unknown_one = 8;
    .unknown_auto_data_nine unknown_two = 9;
    int32 unknown_int = 11;
    int32 unknown_int_one = 12;
    int32 unknown_int_two = 14;
    string unknown_string = 16;
}

message unknown_auto_data {
    string auto_data = 9;
    int32 auto_int = 15;
}

message unknown_auto_data_eight {
    .unknown_repeated_one unknown_re_one = 1;
}

message unknown_repeated_one {
    int32 unknown_rep = 1;
}

message unknown_auto_data_nine {
    int32 unknown_it = 1;
    int32 unknown_itt = 2;
}

我的原始消息是否正确?或者我要怎么写?

还有我在 php 中的请求模型数据:

class RequestModel {

    public $jsonData = [
        "pid" => "0x80c2:0x3a2f",
        "rtext" => "Awesome, This is test.",
        "str" => "5"
    ];
    
    public function getData() {
        return json_encode($this->jsonData);
    }
}

是否有(在线/软件/方法)任何工具来解码 grpc/protobuf 请求/响应可读数据,如上述 json 数据?

\x01\x00\x00\x05\xc0\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\xcdVM\x88\x1be\x18\xced\xdb\xed:E\xba.......

【问题讨论】:

    标签: binary-data proto protocol-buffers protocol-buffers-3


    【解决方案1】:

    要将二进制 protobuf 数据转换为可读形式,请使用 protoc --decode_raw。输出将没有任何字段名称,因为它们不是有线格式的一部分。这是来自https://github.com/pawitp/protobuf-decoder 的示例,它还提供了在线服务来做同样的事情。

    echo -en "\x0a\x2f\x0a\x08\x4a\x6f\x68\x6e\x20\x44\x6f\
    \x65\x10\x01\x1a\x10\x6a\x6f\x68\x6e\x40\x65\x78\x61\x6d\
    \x70\x6c\x65\x2e\x63\x6f\x6d\x22\x0f\x0a\x0b\x31\x31\x31\
    \x2d\x32\x32\x32\x2d\x33\x33\x33\x10\x01\x0a\x1e\x0a\x08\
    \x4a\x61\x6e\x65\x20\x44\x6f\x65\x10\x02\x1a\x10\x6a\x61\
    \x6e\x65\x40\x65\x78\x61\x6d\x70\x6c\x65\x2e\x63\x6f\x6d"\
     | protoc --decode_raw
    
    1 {
      1: "John Doe"
      2: 1
      3: "john@example.com"
      4 {
        1: "111-222-333"
        2: 1
      }
    }
    1 {
      1: "Jane Doe"
      2: 2
      3: "jane@example.com"
    }
    

    如果您的数据有 .proto 文件,请改用 protoc --decode。

    echo -en "\x0a\x2f\x0a\x08\x4a\x6f\x68\x6e\x20\x44\x6f\
    \x65\x10\x01\x1a\x10\x6a\x6f\x68\x6e\x40\x65\x78\x61\x6d\
    \x70\x6c\x65\x2e\x63\x6f\x6d\x22\x0f\x0a\x0b\x31\x31\x31\
    \x2d\x32\x32\x32\x2d\x33\x33\x33\x10\x01\x0a\x1e\x0a\x08\
    \x4a\x61\x6e\x65\x20\x44\x6f\x65\x10\x02\x1a\x10\x6a\x61\
    \x6e\x65\x40\x65\x78\x61\x6d\x70\x6c\x65\x2e\x63\x6f\x6d"\
     | protoc --decode=AddressBook addresses.proto
    
    addresses {
      name: "John Doe"
      id: 1
      email: "john@example.com"
      numbers {
        number: "111-222-333"
        id: 1
      }
    }
    addresses {
      name: "Jane Doe"
      id: 2
      email: "jane@example.com"
    }
    

    address.proto 看起来像这样:

    syntax = "proto3";
    
    message AddressBook {
        repeated Address addresses = 1;
    }
    
    message Address {
        string name = 1;
        int32 id = 2;
        string email = 3;
        repeated Number numbers = 4;
    }
    
    message Number {
        string number = 1;
        int32 id = 2;
    }
    

    您也可以使用protoc 将文本表示编码为二进制变体:

    echo 'addresses { name: "Mike", email: "mike@mike.us" }' | protoc --encode=AddressBook addresses.proto > addressbook.bin 
    
    cat addressbook.bin | protoc --decode=AddressBook addresses.proto
    
    addresses {
      name: "Mike"
      email: "mike@mike.us"
    }
    

    【讨论】:

    • 抱歉回复晚了...你用什么程序转换这段代码echo -en "\x0a\x2f\x0a\x08\x4a\x6f\x68\x6e\x20\x44\x6f\ \x65\x10\x01\x1a\x10\x6a\x6f\x68\x6e\x40\x65\x78\x61\x6d\ \x70\x6c\x65\x2e\x63\x6f\x6d\x22\x0f\x0a\x0b\x31\x31\x31\ \x2d\x32\x32\x32\x2d\x33\x33\x33\x10\x01\x0a\x1e\x0a\x08\ \x4a\x61\x6e\x65\x20\x44\x6f\x65\x10\x02\x1a\x10\x6a\x61\ \x6e\x65\x40\x65\x78\x61\x6d\x70\x6c\x65\x2e\x63\x6f\x6d"\ | protoc --decode_raw
    • protobuf 编译器,protoc. grpc.io 有安装说明:grpc.io/docs/protoc-installation
    猜你喜欢
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    • 2011-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多