1

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

derrend
  • 4,176
  • 4
  • 27
  • 40
  • So what's wrong? What happens? What errors do you get? – Daniel Roseman Jun 13 '14 at 09:17
  • I get a server error 500 but I'm sending the signal from the bitcoin client so I can't see the http response page to debug it. I would like some advice on how to handle the request object. Can someone show me an example of how you would extract information from a field inside the response please? – derrend Jun 13 '14 at 09:47
  • The GAE log, accessible from the admin console, shows you exactly what requests are made and logs all the errors. But you'll need to explain exactly what you're trying to do: `request` is an HTTP request object, it's not a string that you parse into a protobuf. What are you sending from Bitcoin? – Daniel Roseman Jun 13 '14 at 09:54
  • The payment part of a payment request object from the client which I believe would be a serialised string. Is there a way I have to format the request `str(request)` or something similar perhaps? Also I am not using google app engine, it is just my local network. – derrend Jun 13 '14 at 10:06
  • No, there's no point converting it to a string, because it represents the entire HTTP request. Presumably you are POSTing something from Bitcoin, and you'll need to extract the relevant parts to convert to a protobuff, but you need to give more details on what is being sent. Is it a form-encoded POST? Is it a JSON string? And if you're using Django locally, you should see errors in the Django runserver console. – Daniel Roseman Jun 13 '14 at 10:18
  • `"POST /protoresponse/ HTTP/1.1" 500 57793` is what the console is giving me. I am receiving a payment object in the form of a protobuff which I believe is a serialized binary, I must admit I'm not totally sure of the technical terminology in all this :/ – derrend Jun 13 '14 at 10:38

1 Answers1

3

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")
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Thank you for the help, I thought I would have more than one mistake which is why I wanted to get some experienced eyes on it. I will update as soon as I try the changes :) – derrend Jun 13 '14 at 10:49
  • Using the modified code I have posted into the original question I have been able to echo some of the variables into a file for inspection, at the moment I'm not getting past the `f.write('%s\n' % request.body)`, when I cat the file I get "start" followed by "" then the file stops. It seems the `ParseFromString` method really is expecting a string perhaps? – derrend Jun 13 '14 at 12:10
  • But that's to be expected, the serialized protobuf is not expected to be in readable form. See [the protobuf docs](https://developers.google.com/protocol-buffers/docs/pythontutorial): "Note that the bytes are binary, not text; we only use the str type as a convenient container." – Daniel Roseman Jun 13 '14 at 12:14
  • Yes I was just looking at that myself, This one has had me stumped for a few days now :( – derrend Jun 13 '14 at 12:23
  • But you're not saying how you are still stumped. What else is wrong? – Daniel Roseman Jun 13 '14 at 12:30
  • Sorry, I'm still getting the `"POST /protoresponse/ HTTP/1.1" 500 58358` error, the file the function writes to using the modified script in the opening question the script only makes it as far as line `xpo = payments_pb2.Payment.ParseFromString(request.body)` because there is no output in the debug file regarding variable `xpo`. – derrend Jun 13 '14 at 12:39
  • 2
    Apologies, you should be using `FromString` not `ParseFromString`. – Daniel Roseman Jun 13 '14 at 13:44