Sends a message to a specified queue.
Usage notes
Base64-encode the message body before sending. Messages with non-Base64-encoded bodies or unsupported characters cause request failures.
A message sent without a delay is immediately available for consumption. To defer consumption, set
DelaySecondsto a value greater than 0. The message enters the Delayed status and transitions to Active after the specified delay period.If you set
DelaySecondsat both the message level and the queue level, the message-level value takes precedence.
Authorization
By default, only an Alibaba Cloud account can call this operation. To allow a RAM user to call this operation, grant the required permissions. For more information, see Permission policies and examples.
| Name | Value |
|---|---|
| API | SendMessage |
| Action | mns:SendMessage |
| Resource | acs:mns:$region:$accountid:/queues/$queueName/messages |
Request syntax
POST /queues/$queueName/messages HTTP/1.1
Host: $AccountId.mns.<region>.aliyuncs.com
Date: <date>
Content-Length: <content-length>
Content-Type: text/xml;charset=utf-8
x-mns-version: 2015-06-06
Authorization: MNS <AccessKeyId>:<Signature><?xml version="1.0" encoding="UTF-8"?>
<Message xmlns="http://mns.aliyuncs.com/doc/v1/">
<MessageBody>Base64-encoded message content</MessageBody>
<DelaySeconds>0</DelaySeconds>
<Priority>1</Priority>
</Message>Operation-specific request headers: None.
Request body parameters
| Parameter | Type | Required | Example | Description |
|---|---|---|---|---|
| MessageBody | String | Yes | This is a test message | The body of the message. Base64-encode this value before sending. |
| DelaySeconds | Integer | No | 60 | The period in seconds before the message becomes available for consumption. Valid values: 0 to 604800. Default value: 0. |
Response syntax
HTTP/1.1 201 Created
Content-Length: <content-length>
Content-Type: text/xml;charset=utf-8
x-mns-request-id: <request-id>
x-mns-version: 2015-06-06<?xml version="1.0" encoding="UTF-8"?>
<Message xmlns="http://mns.aliyuncs.com/doc/v1/">
<MessageId>5F290C926D472878-2-14D9529A8FA-20000****</MessageId>
<MessageBodyMD5>C5DD56A39F5F7BB8B3337C6D11B6****</MessageBodyMD5>
<ReceiptHandle>1-ODU4OTkzNDU5My0xNDM1MTk3NjAwLTItNg==</ReceiptHandle>
</Message>Operation-specific response headers: None.
Response body parameters
| Parameter | Type | Example | Description |
|---|---|---|---|
| MessageId | String | 5F290C926D472878-2-14D9529A8FA-20000**** | The unique ID of the message in the queue. |
| MessageBodyMD5 | String | C5DD56A39F5F7BB8B3337C6D11B6**** | The MD5 hash of the message body. Use this value to verify message integrity. |
| ReceiptHandle | String | 1-ODU4OTkzNDU5My0xNDM1MTk3NjAwLTItNg== | The receipt handle of the message. Returned only for delayed messages. |
Examples
Send a delayed message
The following request sends a message with a 60-second delay to a queue. The message enters the Delayed status and becomes consumable after 60 seconds.
Request
POST /queues/$queueName/messages HTTP/1.1
Host: $AccountId.mns.cn-hangzhou.aliyuncs.com
Date: Wed, 18 Mar 2012 12:00:00 GMT
Content-Length:500
Content-Type:text/xml;charset=utf-8
x-mns-version: 2015-06-06
Authorization: MNS 15B4D3461F177624206A:xQE0diMbLRepdf3YB+FIEX****<?xml version="1.0" encoding="UTF-8"?>
<Message xmlns="http://mns.aliyuncs.com/doc/v1/">
<MessageBody>This is a test message</MessageBody>
<DelaySeconds>60</DelaySeconds>
<Priority>1</Priority>
</Message>Response
HTTP/1.1 201 Created
Content-Length:120
Content-Type:text/xml;charset=utf-8
x-mns-request-id:512B2A634403E52B1956****
x-mns-version: 2015-06-06<?xml version="1.0" encoding="UTF-8"?>
<Message xmlns="http://mns.aliyuncs.com/doc/v1/">
<MessageId>5F290C926D472878-2-14D9529****-200000001</MessageId>
<MessageBodyMD5>C5DD56A39F5F7BB8B3337C6D11B6****</MessageBodyMD5>
<ReceiptHandle>1-ODU4OTkzNDU5My0xNDM1MTk3NjAwLTItNg==</ReceiptHandle>
</Message>Error codes
The following table lists error codes specific to this operation. For common error codes, see Common error codes.
| Error code | Error message | HTTP status code | Description | Solution |
|---|---|---|---|---|
| QueueNotExist | The queue name you provided is not exist. | 404 | The specified queue does not exist. | Verify the queue name and create the queue if it does not exist. |
| MalformedXML | The XML you provided was not well-formed. | 400 | The request body contains invalid XML. | Check the XML format and make sure it conforms to the request syntax. |
| InvalidArgument | The value of Element should between Low and High seconds/bytes. | 400 | A parameter value is out of range. | Check that DelaySeconds is between 0 and 604800, and that all parameter values are within their valid ranges. |