Shaju Nr
Shaju Nr

Reputation: 358

Unable to send messageAttributes to AWS SQS using node js

I was trying to send message to AWS SQS using Node Js.For that I installed the npm package aws-sdk. I need to send a json array as message attribute and its format is

{"Header": {"OrganizationName": "testOrg","TYPE": "TestMsg", "UserName": "TestUser"}}

but this format does not allow me to send message

    var params = {
      DelaySeconds: 10,
      MessageAttributes: {
        "Title": {
          DataType: "String",
          StringValue: "The Whistler"
        },
        "Author": {
          DataType: "String",
          StringValue: "John Grisham"
        },
        "WeeksOn": {
          DataType: "Number",
          StringValue: "6"
        }
      },
      MessageBody: "Information about current NY Times fiction bestseller for week of 12/11/2016.",
      // MessageDeduplicationId: "TheWhistler",  // Required for FIFO queues
      // MessageId: "Group1",  // Required for FIFO queues
      QueueUrl: "SQS_QUEUE_URL"
    };
sqs.sendMessage(params, function(err, data) {

  if (err) {

   console.log("Error", err);

  } else {

    console.log("Success", data.MessageId);

  }

How to send JSON array in Message Attribute ?

Upvotes: 3

Views: 2914

Answers (1)

davidgyoung
davidgyoung

Reputation: 64995

That format is correct, and I can confirm I have successfully sent messageAttributes using that format using the SDK for NodeJS.

You may find that the issue is on the receiving side. The receiver does not get attributes unless you specify which attributes you want to receive in the messageAttributeNames on the ReceiveMessageRequest. The specific syntax to do this differs by language SDK, and on the Java and Swift SDKs you can supply "All" as the attributeNames to get all attributes. For my Swift SDK, I specified this with receiveMsgRequest.messageAttributeNames = ["All"], then I started receiving the attributes successfully.

Also, don't confuse message.attributes with message.messageAttributes on the receiver side. The former is for system attributes. The latter is what you want.

Upvotes: 3

Related Questions