'How do I read a JSON string from the Message Text field in an Azure Storage queue (using an Azure function)?

I am trying to bind an Azure function to a queue and get the following error:

enter image description here

The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters or an illegal character among the padding characters

This is a sample of what the queue looks like:

enter image description here

The following is the code being used to attempt to read the queue:

Code:

namespace GetInvoicePayload
{
    public static class LoadtoDB
    {
        [FunctionName("LoadtoDB")]
        public static void Run([QueueTrigger("incoming", Connection = "ConnectionStrings")] string myQueueItem, ILogger log)
        {

            //string json = JsonConvert.SerializeObject(myQueueItem);

                //byte[] json = Encoding.ASCII.GetBytes(myQueueItem);
                File.WriteAllText("C:\\dspl\\WriteLines.txt", myQueueItem);


            log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");
        }
    }
}

the JSON string is being serialised through the following code snippet (serialising an ExpandoObject)

string queueMessage = JsonConvert.SerializeObject(queueMessageE);

            QueueClient queueClient = new QueueClient(connectionString, "incoming");

            if (queueClient.Exists())
            {
                // Send a message to the queues
                queueClient.SendMessage(queueMessage);
            }

EDIT: I have added the following based on the comment below but still get the same error message. I have checked and the queue data is correct.

enter image description here

This is the postman payload:

enter image description here

and what the queue receives (now in the poison queue because of the error):

enter image description here



Solution 1:[1]

The reason you are getting this error is because Queue Triggered Azure Functions expect the message body to be bas64 encoded.

From this link:

Functions expect a base64 encoded string. Any adjustments to the encoding type (in order to prepare data as a base64 encoded string) need to be implemented in the calling service.

What you would need to do is send the message bas64 encoded to the queue and then decode it back in your function code.

Something like:

queueClient.SendMessage(Convert.ToBase64String(Encoding.UTF8.GetBytes(queueMessage)));

and

string messageBody = Encoding.UTF8.GetString(Convert.FromBase64String(myQueueItem));

Solution 2:[2]

I have figured this one out:

Mistake 1: I had accidentally used the Service Bus function template instead of the Queue triggered one. This helped solve the issue of accessing the message in the queue. Tested the boilerplate method and the following code line returned the message:

log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");

Mistake 2: Interestingly the reverse encoding of the message happens through the call to the queue itself. There is no need to convert the Base64 string back (it actually throws and error). The string that is returned is already decoded.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gaurav Mantri
Solution 2 Zafissa