0

I am using protobuf and need to send converted bytes with api request, and need to decode again on server, string will be like this:

\b\xC0\xB3\xB9\xDD\xFC\x1C\x12XBalance debited with 62.0 Expiry Date is 09-11-2016 09:10:00 Remaining Balance is 1490.0\x1A\x0FDebited Balance\"XBalance debited with 62.0 Expiry Date is 09-11-2016 09:10:00 Remaining Balance is 1490.0(\x99\x9C\xCE\xBF\x05

How can I send this type of the request and get properly on server?

Or anyone help me to send information using protobuf.

When I send string in body in then it replace form

\x99\x9C\xCE\xBF\x05

to

x99x9CxCExBFx05

and when send in headers it replaced like

\\x99\\x9C\\xCE\\xBF\\x05

Thanks

Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
Thorin
  • 2,004
  • 1
  • 19
  • 30
  • Whether you do want the exact same string on the server, use `base64`. – Aleksei Matiushkin Jan 03 '17 at 06:01
  • you mean need to encode this in base64 first then send to server and decode there? – Thorin Jan 03 '17 at 06:04
  • I would go this way, yes. It’s the simplest and safest one. – Aleksei Matiushkin Jan 03 '17 at 06:05
  • @mudasobwa any other way to do this? with google protobuf? – Thorin Jan 03 '17 at 06:07
  • after decoding with base64 it also produce the string with double backward slash like: \\b\\xC0\\xB3\\xB9\\xDD\\xFC\\x1C\\, it seems problem with ruby online encode decode seems working – Thorin Jan 03 '17 at 06:13
  • “after decoding with base64 it also produce the string with double backward slash”—it does not. You mix up how ruby prints strings out. Try `original = '\x99\x9C\xCE\xBF\x05'; original == Base64.decode64(Base64.encode64(original))`. What you think are double quotes is in fact how ruby prints strings out. `puts original #⇒ \x99\x9C\xCE\xBF\x05`, but `p original #⇒ "\\x99\\x9C\\xCE\\xBF\\x05"`. Also, google the difference between single-quoted strings and double-quoted strings in ruby. – Aleksei Matiushkin Jan 03 '17 at 06:25
  • @mudasobwa, It seems problem with encoding when encoding using ruby for the same string it returns this: CMCzud38HBJYQmFsYW5jZSBkZWJpdGVkIHdpdGggNjIuMCBFeHBpcnkgRGF0\nZ...... and I can decode it as original as well and when encode using online tool it return like XGJceEMwXHhCM1x4QjlceEREXHhGQ1x4MUNceDEyWEJhbGFu..... – Thorin Jan 03 '17 at 06:33

1 Answers1

0

Besides what I have written in comments, the ruby protobuf binding home contains a perfect example of how it’s to be achieved:

require 'google/protobuf'

# generated from my_proto_types.proto with protoc:
#  $ protoc --ruby_out=. my_proto_types.proto
require 'my_proto_types'

mymessage = MyTestMessage.new(:field1 => 42, :field2 => ["a", "b", "c"])
mymessage.field1 = 43
mymessage.field2.push("d")
mymessage.field3 = SubMessage.new(:foo => 100)

# ⇓⇓⇓ HERE ⇓⇓⇓
encoded_data = MyTestMessage.encode(mymessage)
# ⇑⇑⇑ HERE ⇑⇑⇑
decoded = MyTestMessage.decode(encoded_data)
assert decoded == mymessage

Everything you need is to encode the bytes before sending:

mymessage = MyTestMessage.new(message: '\x99\x9C\xCE\xBF\x05')
encoded_data = MyTestMessage.encode(mymessage)
# OR encoded_data = MyTestMessage.encode_json(mymessage)

and now send encoded_data to your recipient.

Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
  • Yes I see this example but string will be generated from android side and come to ruby with an api call thats why I have to send string – Thorin Jan 03 '17 at 06:36
  • `MyTestMessage.encode(mymessage)` produces string, does not it? – Aleksei Matiushkin Jan 03 '17 at 06:36
  • yes it produce the string like I have added in my question also decoding is working but problem is that when I send the same string with api not able to receive the same on ruby side – Thorin Jan 03 '17 at 06:56