Sends multiple messages to a queue at a time.

Authorization information

By default, you can use only an Alibaba Cloud account to call this operation. You can use a RAM user to call this operation only after you grant the required permissions to the RAM user The following table describes the authorization information of this operation. For more information, see Permission policies and examples.

Name Value
API BatchSendMessage
Action mns:BatchSendMessage
Resource acs:mns:$region:$accountid:/queues/$queueName/messages

Usage notes

  • You can call this operation to send a maximum of 16 messages to a queue at a time. The maximum size of messages that can be sent at a time is 64 KB. If you do not specify the DelaySeconds parameter when you send a message to the queue, the message is immediately consumed. If you want the message to be consumed on a regular basis, you can specify the DelaySeconds parameter when you send the message to the queue.
  • If you set the DelaySeconds parameter to a value that is greater than 0, the message is in the Delayed state after the message is sent to the queue. The message cannot be consumed until the period that is specified by the DelaySeconds parameter ends and the status of the message changes to Active. If the value of the DelaySeconds parameter for the message and queue is different from the value of the DelaySeconds parameter for the queue, the value of the DelaySeconds parameter for the message takes effect.
  • Before you call this API operation to send a message, encode the message body by using Base64. This prevents unexpected errors due to special characters.

Request

A request consists of the following parts:

  • Request line

    POST /queues/$queueName/messages HTTP/1.1

  • Operation-specific request headers

    None.

  • Request Body

    The request body is in the XML format. The parameters of multiple messages are included in the request body.

    Field Type Required Example Description
    MessageBody String Yes This is test message 1. The body of the message.
    DelaySeconds Integer No 60 The delay period after which the message sent to the queue can be consumed.

    Valid values: 0 to 604800. Unit: seconds.

    Default value: 0.

    Priority Integer No 1 The priority of the message. A message with a higher priority is consumed earlier.

    Valid values: 1 to 16. A smaller value indicates a higher priority.

    Default value: 8.

Response

A response consists of the following parts:

  • HTTP Status Code

    HTTP/1.1 201 Created

  • Operation-specific response headers

    None.

  • Response Body

    The response body is in the XML format. The MessageId and MessageBodyMD5 parameters of multiple messages are returned.

    Parameter Type Example Description
    MessageId String 5F290C926D472878-2-14D9529A8FA-20000**** The ID of the message. The message ID is unique in the queue.
    MessageBodyMD5 String C5DD56A39F5F7BB8B3337C6D11B6**** The MD5 hash of the message body.

Examples

Sample requests

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+FIEXA****

<?xml version="1.0" encoding="UTF-8"?>
<Messages xmlns="http://mns.aliyuncs.com/doc/v1/">
    <Message>
        <MessageBody>This is test message 1.</MessageBody>
        <DelaySeconds>60</DelaySeconds>
        <Priority>1</Priority>
    </Message>
    <Message>
        <MessageBody>This is test message 2.</MessageBody>
        <DelaySeconds>60</DelaySeconds>
        <Priority>1</Priority>
    </Message>
</Messages>           

Sample responses

  • Sample success response if all messages are sent
        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"?>
        <Messages xmlns="http://mns.aliyuncs.com/doc/v1/">
        <Message>
            <MessageId>5F290C926D472878-2-14D9529A8FA-20000****</MessageId>
            <MessageBodyMD5>C5DD56A39F5F7BB8B3337C6D11B6****</MessageBodyMD5>
        </Message>
        <Message>
            <MessageId>5F290C926D472878-2-14D9529A8FA-20000****</MessageId>
            <MessageBodyMD5>377AC5283D8765C9CEE4E0EA353D****</MessageBodyMD5>
        </Message>
        </Messages>                   
  • Sample response if some messages fail to be sent
        HTTP/1.1 500
        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"?>
        <Messages xmlns="http://mns.aliyuncs.com/doc/v1/">
        <Message>
            <MessageId>5F290C926D472878-2-14D9529A8FA-20000****</MessageId>
            <MessageBodyMD5>C5DD56A39F5F7BB8B3337C6D11B6****</MessageBodyMD5>
        </Message>
        <Message>
            <MessageId>5F290C926D472878-2-14D9529A8FA-20000****</MessageId>
            <MessageBodyMD5>377AC5283D8765C9CEE4E0EA353D****</MessageBodyMD5>
        </Message>
        <Message>
            <ErrorCode>InternalError</ErrorCode>
            <ErrorMessage>Internal error.</ErrorMessage>
        </Message>
        </Messages>              

Error codes

Error code Error message HTTP status code Description
QueueNotExist The queue name you provided is not exist. 404 The error message returned because the specified queue does not exist. Create a queue.
MalformedXML The XML you provided was not well-formed. 400 The error message returned because the XML file you provided is malformed.
InvalidArgument The value of Element should between Low and High seconds/bytes. 400 The error message returned because the value of the parameter is invalid. You must specify a valid value as prompted.