Android Question How to send bytearray to json rest api

rubach

Member
Licensed User
Longtime User
Hi,
I have a rest api (json) for an invoice app.
When an invoice it is generate in the app, I need to send it for printer in the server.

The invoice it is an stringbuilder.
I am trying to send the stringbuilder with PostBytes, but it arrives empty or the string.

B4X:
    Dim j As HttpJob
        j.Initialize("SubirImagenTicket",Me)
        Dim printimagen() As Byte="Hello word".GetBytes("UTF8")
        
        j.PostBytes(clParametros.URLApi & apiUrl, printimagen)
        j.GetRequest.SetContentType("application/json")
        j.GetRequest.Timeout=60000
        j.GetRequest.SetContentEncoding("UTF8")
        
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            Dim parser As JSONParser
            parser.Initialize(j.GetString)
            Log(j.GetString)
            Dim root As Map = parser.NextObject
            Dim CodigoError As Int = root.Get("CodigoError")
            If (CodigoError=0) Then
                EjecucionOk=True
            End If
        Else
            Log ("Error:" & j.ErrorMessage)
        End If

        j.Release


This example arrive with value=null to rest api bytearray but int the log request in the server, show "Hello word"

But if I coment the line
B4X:
  j.GetRequest.SetContentType("application/json")
the value it is Byte[0] of the byteArray.

Any have any Idea of how solve this issue?

Others clients of the rest api in C# work well.
 

OliverA

Expert
Licensed User
Longtime User
You are posting bytes, not JSON. What are the API specifications? What is the C# client code that successfully posts?
 
Upvote 0

rubach

Member
Licensed User
Longtime User
Hi,
In C# to send the bytearray I call:

to create the array:
B4X:
byte[] invoice=Encoding.Default.GetBytes(sb.ToString());

to send by call api rest
B4X:
HttpResponseMessage response = await client.PostAsJsonAsync(apiurl, invoice);

and arrive to server something this:
application/json; charset=utf-8 "G0AbYQFJTk5URUdSQUNPTSBTLlIuTC4KSU5OVEVHUkEgU09GVFdBUkU...

the length of the array are aprox. 1500 bytes and the api rest

the api uri it is:
[HttpPost]
public IHttpActionResult PostNewPrintImage(string apikeytoken, byte[] value)

With C# work well, but from Android I am trying:

for example, to create a test bytearray with some words.

B4X:
Dim printimagen() As Byte="Hello word".GetBytes("UTF-8")

-Send by PostBytes EJ:
B4X:
j.PostBytes(clParametros.URLApi & apiUrl,printimagen)
j.GetRequest.SetContentType("application/json; charset=utf-8")
but to the server arrive: application/json; charset=utf-8 Hello word
And the api post value it is byte[] value=null

-Send by saving file to disk and send by PostFile, the same result.
-Send by JSONGenerator in a map, by PostString and arrive:
application/json {"DocInvoice":[24,27,64,27,107,50,32,32,32,32,...]}
In this case changed the parameter to receive this objet in the rest api, and work well if the array it is shot, I do not know in some cases the array it is truncate and fail to recive.

Well I still working int options....
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
and arrive to server something this:
application/json; charset=utf-8 "G0AbYQFJTk5URUdSQUNPTSBTLlIuTC4KSU5OVEVHUkEgU09GVFdBUkU...
Sounds like you need to base64 encode the json and use the base64 string in the upload then.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Upvote 0

OliverA

Expert
Licensed User
Longtime User
You can also use this site to see what your C# client posts. https://ptsv2.com/ Just create a new random "toilet". That will give you an URL you can use with your C# client to do a PostAsJsonAsync with some test data. You would then see the "raw" posting of the PostAsJsonAsync method.
 
Upvote 0
Top