Failed to send odd lenght payload (base64 decoding error)

Hello Orne,
whenever I try to send an oddd lenght payload I receive en error of type “illegal base64 at byte …”

It seems to be related to the fact that the data is []byte type and so the JSON payload is read as base64

Are you sure this is not a base64 encoding issue at your side? Example:

In lora-app-server, mqtt_handler.go:

dec := json.NewDecoder(bytes.NewReader(msg.Payload()))
if err := dec.Decode(&pl); err != nil {
“data_base64”: base64.StdEncoding.EncodeToString(msg.Payload()),
}).Errorf(“handler/mqtt: tx payload unmarshal error: %s”, err)

if the data field sent is for exampe “03” the Decode function returns error:
level=error msg=“handler/mqtt: tx payload unmarshal error: illegal base64 data at input byte 0”

How did you get to the "03" base64 encoded string? E.g. in my above example (url) the array of bytes []byte{1, 2, 3} results in the base64 encoded string "AQID" (encoded to HEX this would be "010203").

Yes, sorry you’re right.

I receive a json with “data”:“03” which is the payload to be transmtitted. The problem is that the unmarshalling expects a base64 payload which shoud be AW==

The application sending the payload is out of my control, so I cannot chenge it to send base64 payload
Shall I change the payload type in String instead of []byte?

I suspect that "data": "03" means that data is encoded as HEX? In that case the only thing you need to do is decode and re-encode to base64. To summarize:

HEX IN => decode HEX to byte array => encode byte array to base64 => base64 OUT

No need to make modifications to LoRa App Server.

Sorry I don’t undertsand.
The situation during a downlink transmission is::
an external app send this JSON payload
“confirmed”: false,
“data”: “03”,
“devEUI”: “xxxxxxxxxxxxxxxx”,
“fPort”: 7,
“reference”: “string”

to MQTT topic

Lora-app-server/mqtt_handler receive this payload and it rties to parse to dataDownPyaload whichi contain a member Data of type [byte]


dec := json.NewDecoder(bytes.NewReader(msg.Payload()))
if err := dec.Decode(&pl); err != nil { …

the Decode function fails because it is assuming the value of Data as encoded base64…but it is not (“03”)

Where shall I put the conversion? The entry point is the payload received by mqtt but I cannot manipulate it becasue it is given as []byte with msg.Payload()

I changed the type of Data inside the dataDownPayload as String and I made a conversion to be able to send payload non base64 encoded such as “03”

Thanks for this post. It helped me a lot!!!
in the go playground link
for Hexa format , i just add “0x” in front of the value and its work well!

1 Like