Android Question PostString vs PostBytes

aeric

Expert
Licensed User
I was reading this post HttpJob - Post and receive bytes and thinking is there any difference between using PostString and PostBytes?

I remembered last time I opened the OkHttpUtils2 b4xlib and saw that PostString eventually calling PostBytes. So why make things more difficult to convert the String into Byte data ?

Does it really make the data smaller in size when transfer to the server if we convert the same data to Bytes?
 

Erel

Administrator
Staff member
Licensed User
So why make things more difficult to convert the String into Byte data ?
Not sure that I understand. If you want to send string use PostString. If you want to send bytes use PostBytes. How is it making anything more difficult?

The fact that the string is converted to bytes internally is not important.
 
Upvote 0

aeric

Expert
Licensed User
Now I want to do the same code to send and receiving bytes to reduce the traffic.

Compare:
B4X:
Http_TbColetaProdutoLocal.PostString(host & "/inserecoletaprodutolocal", JsonData)
Http_TbColetaProdutoLocal.GetRequest.SetContentType("application/json")
with
B4X:
Http_TbColetaProdutoLocal.PostBytes(host & "/inserecoletaprodutolocal_v2", Compressor.CompressBytes(SendData, "zlib"))
Http_TbColetaProdutoLocal.GetRequest.SetContentType("application/octet-stream")

Is it the second code sending less kb of data ?

How is it making anything more difficult?
I think it would requires one to write more lines of code in client app to transform the String data to byte, use compression library and handle the reverse in server by decompress the byte then decode the byte to json. Since OkhttpUtils2 already handled the job, why bother to use PostBytes for the same? Unless there is a benefit over the PostString. I know if some third party API need to use raw data then we have no choice, but I am considering this question if I am the person who create my own API.
 
Upvote 0

aeric

Expert
Licensed User
It's okay. I will stick with PostString. Thanks Erel.
 
Upvote 0

OliverA

Expert
Licensed User
My 5 cents:
Leave compression up to the web server and the client HTTP library. In these day and ages, web servers should be set up to handle compression and negotiate such with the client. By you trying to compress the data, you may actually create more data, since compression of compressed data usually leads to slightly larger sizes. If you are using jServer, then just set GzipEnabled to true before calling the Start method.

OkHttp support for GZIP is mentioned at their site: https://square.github.io/okhttp/
 
Upvote 0

aeric

Expert
Licensed User
Forget about the compression, my initial question is not intended to put it as a consideration.
I apologize to everyone who is confused on the topic title.
I try to rephrase it.

Let say I already have an API https://mydomain/update_order_status/v1
which accept JSON as body. The body may be very long string.

Now, I want to create a version v2 which accept the body in bytes instead. If I understand correctly, the data would be the same as what I get when using v1 except for I need to transform back the bytes into JSON. In this case, there is no difference at all or any point creating a v2.

The question is not really about PostString vs PostBytes. Actually I want to know the best practice to create an efficient or a better way to build the web API.
 
Upvote 0

aeric

Expert
Licensed User
The question should be:
Should I send Clear Text or Raw data?

For now my understanding is that it depends. I will do more experiments on this in my future developments. Maybe I will send a map of strings, raw data and custom type mixed together. I will find out when the project has such requirements.

Thanks all.
 
Upvote 0
Top