goatrenz
goatrenz

Reputation: 35

Amazon SQS send message attributes in php

I need to implement sending messages to SQS with attributes. The body of the message is uploading fine, but I have problem with attributes. Message Attributes require Associative array with Name of the Attribute, Data Type, and Value. I got this kind of error :

AWS HTTP error: Client error: 400 InvalidParameterValue (client): The request must contain non-empty message attribute name.

the function for sending messages:

public function uploadMessage(DataTransferObjectInterface $dataTransferObject)
{

    $command = $this->client->getCommand(
        'SendMessage',
        [
            'QueueUrl' => $this->queueUrl->value(),
            'MessageBody' => $dataTransferObject->getBody()->value(),
            'MessageAttributes' => $dataTransferObject->getAttributes(),

        ]
    );

    $this->client->execute($command);

}

function getAttributes() returns $attributes array

& the test where I run the code

   $attributes = [
   'TestName' =>
            [
            'Name'=>'test',
            'DataType' => 'string',
            'Value' => 'string',

        ]
    ];
    $age = array("Peter" => "35", "Ben" => "9", "Joe" => "43");
    $json = json_encode($age);
    $body = Json::get($json);

    $dto = new DataTransferObject($attributes, $body);

    $uploader = new SQSManager($sqsClient, $queueUrl);
    $uploader->uploadMessage($dto);

How does the $attributes array should look like?

Upvotes: 3

Views: 5245

Answers (1)

giaour
giaour

Reputation: 4061

A fix for this bug is built into version 3.2.1 and greater.

Additionally, your message attributes array doesn't match the format shown in the documentation. Your attributes should look like this:

$attributes = [
    '<attribute name>' => [
        'DataType' => 'String',
        'StringValue' => '<attribute value>',
    ],
];

Upvotes: 6

Related Questions