How to capture and modify Google Protocol Buffers

2019-06-10 18:12发布

问题:

Here is a link to the proto file.

Please can someone help me understand how to make this work:

from django.views.decorators.csrf import csrf_exempt
from bitchikun import payments_pb2

@csrf_exempt
def protoresponse(request):
    xpo = payments_pb2.Payment.ParseFromString(request)
    t = type(xpo)

    xpa = request.PaymentACK
    xpa.payment = xpo.SerializeToString()
    xpa.memo = u'success'
    return HttpResponse(xpa.SerializeToString(), content_type="application/octet-stream")

All input appreciated :)

回答1:

OK so I think I understand what is happening now. You have a system which is POSTing a serialized protobuf to your Django app, and you need to return another protobuf in response.

In Django, you can access the data from a POST in request.body. That is probably what you need to pass to ParseFromString.

You have some other errors too: you refer to request.PaymentACK, which doesn't exist - you mean payments_pb2.PaymentACK - and you never actually instantiate it. Also, you are trying to pass the serialized version of the original request protobuf to that response one, when you should be passing the actual message.

So, altogether it would look like this:

xpo = payments_pb2.Payment.FromString(request.body)
xpa = payments_pb2.PaymentACK()
xpa.payment = xpo
xpa.memo = u'success'
return HttpResponse(xpa.SerializeToString(), content_type="application/octet-stream")