-2

I am passing an encoded file as a http:Request request for a ballerina REST service. I need to decode the binary payload of the request using Base64 in order to upload it to a file server. I was unable to find a way to decode a byte[] using Base64. Here is my sample code.

resource function post\-file(http:Request request) returns string|error {
    byte[]|http:ClientError binaryPayload = request.getBinaryPayload();
    if binaryPayload is http:ClientError {
        return error("Error in payload");
    }

    // Now I need to decode the binaryPayload into a byte[]
    // Tried following options. But seems like those options are not available in Swan Lake Update 3

    // byte[] decodedPayload = check encoding:base64Decode(binaryPayload);
    // byte[] decodedPayload = io:base64Decode(encodedPayload);


    // How to do this in a correct way?

    return "success";
}

How can I do this ?

shayanmalinda
  • 1,925
  • 3
  • 12
  • 23

1 Answers1

1

You can use the ballerina/mime library for encoding/decoding byte[] values.

import ballerina/mime; // Make sure to import the mime package

// ...

byte[] decodedPayload = check mime:base64Decode(binaryPayload);
ThisaruG
  • 3,222
  • 7
  • 38
  • 60