How can I send this type of string with api call

258 Views Asked by At

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

1

There are 1 best solutions below

3
On

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.