how to turn a map to a byte array?
# announcements
i
how to turn a map to a byte array?
g
map to byte array? how does it should work in theory?
i
there’s a post example by python, it uses requests, there is a parameter is
data = a_dict
in that requests, and I thin this a_dict is converted to bytes by requests, ‘cause data’s parameter is bytes
and now I rewrite that example with kotlin
I use httpurlconnection
g
what kind parameter?
HTTP has a lot of request params: get, post, headers, cookies, any kind of body type
so just set map data to particular API
i
data=getBody(r"7.ogg") this getBody will return a dict but data suppose to take bytes
g
So, you just want to send binary data as JSON?
Gson should allow to convert map to Json, so at first glance I don't see problem
But looks that original python script doesn't send it as JSON, but as
key=value
string, at least how I understand python request api
So, just create such string from map (you actually don't need map, body can just return string, but if you want, you also can do it from Map by just converting each entity to string and joining)
And than write string to output
But in general it's not really good approach for binary data, but it what original sample does
i
@gildor yeah, I write that “audio=“ + thatStringFromFile to output, It works! it’s weird that python example use a dict, and thank you!
g
Yeah, looks too opionated for such generic api, probably it checks request type to understand body format, but still
229 Views