Reputation: 5074
I've implemented the following protobuf based protocol
message singleConfig {
string configName = 1;
string configValue = 2;
}
message currentConfig {
repeated singleConfig conf = 1;
}
message HttpRequest {
string osVersion = 1;
string productVersion = 2;
currentConfig config = 3;
}
On my http python server, I expect to get http post requests from body that conform this protocol.
So upon incoming http post request, the body contents arrived and seems valid (I can identify the fields' values from the text)
b'2@\n\x0611.5.1\x12\x061.0(1)\x1a.\n,\n\x08file.json\x12 ecf1c21c77a419f8f7dbfb714a806166'
Here's the code that parse the http request. notice that ParseFromString
accept 'bytes' formatted input. The parsing finish without any exception so I assume it went alright...
message = HttpRequest()
message.ParseFromString(data)
However, an attempt to access each one of the fields in the protobuf structure reveals empty value :
message.osVersion
''
Any idea what's wrong with the parsing ?
Upvotes: 0
Views: 647
Reputation: 40061
I think your incoming content is incorrect:
from google.protobuf.message import DecodeError
import test_pb2
b = b"2@\n\x0611.5.1\x12\x061.0(1)\x1a.\n,\n\x08file.json\x12 ecf1c21c77a419f8f7dbfb714a806166"
m = test_pb2.HttpRequest()
try:
m.ParseFromString(b)
except DecodeError as err:
print(err)
Yields: Error parsing message
But:
import requests
import test_pb2
m = test_pb2.HttpRequest()
m.osVersion="osVersion"
m.productVersion="productVersion"
c = m.config.conf.add()
c.configName="configName"
c.configValue="configValue"
print(m)
s = m.SerializeToString()
print(s)
print(s.hex())
url = "https://en22tibjys2gf.x.pipedream.net"
requests.post(url,data=s)
Yields:
osVersion: "osVersion"
productVersion: "productVersion"
config {
conf {
configName: "configName"
configValue: "configValue"
}
}
b'\n\tosVersion\x12\x0eproductVersion\x1a\x1b\n\x19\n\nconfigName\x12\x0bconfigValue'
0a096f7356657273696f6e120e70726f6475637456657273696f6e1a1b0a190a0a636f6e6669674e616d65120b636f6e66696756616c7565
And, if you check the request.bin URL, you'll see that the raw body is:
0000000 0a 09 6f 73 56 65 72 73 69 6f 6e 12 0e 70 72 6f
0000010 64 75 63 74 56 65 72 73 69 6f 6e 1a 1b 0a 19 0a
0000020 0a 63 6f 6e 66 69 67 4e 61 6d 65 12 0b 63 6f 6e
0000030 66 69 67 56 61 6c 75 65
The hex sent (0a096f7...7565
) matches that received by the request.bin service.
You may also paste (0a096f7...
) into Marc Gravell's protobuf decoder
Upvotes: 1