How to capture and modify Google Protocol Buffers in a Django view?

旧时模样 提交于 2019-12-08 03:56:17

问题


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")


来源:https://stackoverflow.com/questions/24200734/how-to-capture-and-modify-google-protocol-buffers-in-a-django-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!