4

I am using azure-storage node module. I would like to send JSON on my queue and get it into my queue on azure function.

I send message in queue. I stringify my message to put in queue.

// This is my service who send message to queue via node lib azure-storage
       const queueMsg = {
              userId,
              token: tokenNotif
            };
            queueSvc.createMessage(Config.REGISTRATION_FCM_PUSH_NOTIFICATION_QUEUE, JSON.stringify(queueMsg), (err) => {
              if (!error) {
                this._logger.info(`User ${userId} has register this push notification token`);
                resolve(true);

              } else {
                reject(false);
              }
            });

And in the queue function i have an error because the function think isn't a string and push the message text on xx-queue-poison {"userId":"a6c8a103-dacc-4b15-bffd-60693105f131","token":"xxxx"}

I don't know why the quote is replaced by ASCII code on queue ?

I have tested something else! From my service i call a Http Azure function, and this function call the Queue Storage and it's work by this way :s ..

The HttpTrigger function call queue context.bindings.notificationQueue = [{ userId: name, token }];

And queue received data context.log(`Received userId ${queueItem.userId} :: ${queueItem.token}`);

Why by using HttpTrigger function to QueueTrigger function it's working, but when i am using the lib "azure-storage" is not working ?

Thx

1
  • 4
    I found ! Instead of stringify the object i convert it into b64. Buffer.from(JSON.stringify(queueMsg)).toString('base64') and it's working Commented Jun 26, 2019 at 8:59

1 Answer 1

3

I don't know why the quote is replaced by ASCII code on queue ?

Basically the SDK is converting the string message to make it XML safe. If you look at the code in the SDK, by default it uses TextXmlQueueMessageEncoder as the message encoder. The encode function replaces " with " to make it XML safe.

From the SDK code (partial code snippets):

QueueService.prototype.createMessage = function (queue, messageText, optionsOrCallback, callback) {
  var userOptions;
  azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; });

  validate.validateArgs('createMessage', function (v) {
    v.string(queue, 'queue');
    v.queueNameIsValid(queue);
    v.callback(callback);
  });

  var xmlMessageDescriptor = QueueMessageResult.serialize(messageText, this.messageEncoder);

function TextXmlQueueMessageEncoder(){
}
util.inherits(TextXmlQueueMessageEncoder, QueueMessageEncoder);

/**
 * Encode utf-8 string by escaping the xml markup characters.
 * @this TextXmlQueueMessageEncoder
 * 
 * @param   {string}    [input]               The target to be encoded.
 * 
 * @return {string}
 */
TextXmlQueueMessageEncoder.prototype.encode = function(input){
  return input.replace(/&/gm, '&')
    .replace(/</gm, '&lt;')
    .replace(/>/gm, '&gt;')
    .replace(/"/gm, '&quot;')
    .replace(/'/gm, '&apos;');
};

One possible solution would be to convert the string to a base64 encoded string as you suggested. However if you're using the SDK to retrieve the messages, you should not see these &quot; in your message body as the SDK takes care of decoding the message.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.