how to convert protobuf readable request to proto message

2.3k Views Asked by At

I am playing with google api for educational purpose to learn protobuf connection. But i am getting 400 Bad Request when i sending post request. I hope so my protobuf message have problem and don't understand how to i write 0: 0 message in proto file. I am using PHP Protobuf library to serialization data.

This is readable/decoded grpc post request data:

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 {

I created this proto messages by following above grpc request data:

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

Is my proto messages are correct? or How will i write it?

Also my request model data in php:

class RequestModel {

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

Is there (online/software/method) any tools to decode grpc/protobuf request/response to readable data like above json like data?

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

There are 1 best solutions below

2
On

To convert binary protobuf data to a readable form, use protoc --decode_raw. The output will not have any field names, as those are not part of the wire format. Here is an example from https://github.com/pawitp/protobuf-decoder, which also provides an online-service to do the same thing.

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: "[email protected]"
  4 {
    1: "111-222-333"
    2: 1
  }
}
1 {
  1: "Jane Doe"
  2: 2
  3: "[email protected]"
}

If you have the .proto file for your data, use protoc --decode instead.

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: "[email protected]"
  numbers {
    number: "111-222-333"
    id: 1
  }
}
addresses {
  name: "Jane Doe"
  id: 2
  email: "[email protected]"
}

Where addresses.proto looks like this:

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

You can also use protoc to encode from the textual representation to the binary variant:

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