Using Protocol Buffer I serialize data and transfer it by adding it as a parameter to HTTP GET requests being sent to Flask. The deserialization of the data sometimes fails depending on the content of the parameters (e.g. if the url parameter contains "%F0").
I tried using different charsets for encoding/decoding and also tried to add a proper header to the request setting content-type to application/x-protobuf.
This is the code of flask handling the incoming GET request.
def _ai_request_stub(min_params: List[str], on_parameter_available: Callable[[], Response]) -> Response:
"""
This stub is designed for GET requests.
"""
from flask import request
missing_params = list(filter(lambda p: p not in request.args, min_params))
if missing_params:
return Response(response="The request misses one of the parameters [\"" + "\", ".join(missing_params) + "\"]",
status=400, mimetype="text/plain")
else:
return on_parameter_available()
@app.route("/ai/control", methods=["GET"])
def control():
def do() -> Response:
from aiExchangeMessages_pb2 import Control
from flask import request
control_msg = Control()
control_msg.ParseFromString(request.args["control"].encode())
return Response(response="Fine", status=200, mimetype="application/x-protobuf")
return _ai_request_stub(["control"], do)
This is the code creating and sending the GET request.
class AIExchangeService:
from aiExchangeMessages_pb2 import SimStateResponse, DataRequest, DataResponse, Control, Void, AiID
from typing import Dict, AnyStr, Any
def __init__(self, host: str, port: int):
self.host = host
self.port = port
[...]
def _do_get_request(self, address: str, params: Dict[str, AnyStr]) -> HTTPResponse:
"""
:return: The response object of the request
"""
from urllib.parse import urlencode
from http.client import HTTPConnection
connection = HTTPConnection(host=self.host, port=self.port)
print(params)
connection.request("GET", address + "?" + urlencode(params),
headers={"content-type": "application/x-protobuf; charset=utf-8"})
return connection.getresponse()
def control(self, commands: Control) -> Void:
response = self._do_get_request("/ai/control", {"control": commands.SerializeToString()})
if response.status == 200:
print("Controlled")
else:
print(response.status)
print(response.reason)
[...]
This is the protobuffer code showing the structure of the Control object.
message AiID {
[...]
}
message Control {
message AvCommand {
double accelerate = 1;
double steer = 2;
double brake = 3;
}
enum SimCommand {
RESUME = 0;
FAIL = 1;
CANCEL = 2;
}
AiID aid = 1;
oneof command {
AvCommand avCommand = 2;
SimCommand simCommand = 3;
}
}
The call commands.SerializeToString()
in the second snippet in method control(...)
yields b'\n\x13\n\n\n\x08fancySid\x12\x05\n\x03ego\x12\t\t\x00\x00\x00\x00\x00\x00\xf0?'
. The evaluation of address + "?" + urlencode(params)
in the second snippet in method _do_get_request(...)
produces the output /ai/control?control=%0A%13%0A%0A%0A%08fancySid%12%05%0A%03ego%12%09%09%00%00%00%00%00%00%F0%3F
which seems to be the same but urlencoded.
When sending this GET request to flask request.args["control"]
in the first snippet in method control()
yields '\n\x13\n\n\n\x08fancySid\x12\x05\n\x03ego\x12\t\t\x00\x00\x00\x00\x00\x00�?'
which is not the same serialized string anymore. Trying to deserialize this string fails with the error google.protobuf.message.DecodeError: Error parsing message
.
What can I do to make flask read the parameter correctly?
The GET request is not made for shipping huge raw binary data. Instead a POST request has to be used. And indeed a post request works just fine.