Subscribe

Updated at:

Call the Subscribe operation to create a subscription for a topic.

Authorization information

By default, only Alibaba Cloud accounts can call this operation. Resource Access Management (RAM) users can call this operation only after they are granted the required permissions. The following table describes the authorization information for this operation. For more information, see Authorization policies and examples.

Name

Value

API

Subscribe

RAM authorization operation

mns:Subscribe

Resource

acs:mns:$region:$accountid:/topics/$topicName/subscriptions/$subscriptionName

Usage notes

  • A subscription name is a string of up to 255 characters. It must start with a letter or a digit. The rest of the name can contain letters, digits, and hyphens (-).

  • You must specify an endpoint when you create a subscription. Otherwise, the request is invalid.

Request message

A request message consists of a request line, HTTP headers, and a message body:

  • Request line

    PUT /topics/$TopicName/subscriptions/$SubscriptionName HTTP/1.1

  • Request-specific headers

    None.

  • Request body

    The request body is in XML format and contains the properties that are required to create the subscription.

    Parameter Name

    Type

    Required

    Example

    Description

    Endpoint

    String

    Yes

    http://example.com

    The following four types of endpoints are supported:

    • HttpEndpoint: Must start with `http://`.

    • QueueEndpoint: The format is `acs:mns:{REGION}:{AccountID}:queues/{QueueName}`.

    • MailEndpoint: The format is `mail:directmail:{MailAddress}`.

    • SmsEndpoint: The format is `sms:directsms:anonymous` or `sms:directsms:{Phone}`.

    FilterTag

    String

    No

    important

    The tag for message filtering in the subscription. Only messages with the same tag are pushed.

    The value must be a string of up to 16 characters.

    By default, messages are not filtered.

    NotifyStrategy

    Boolean

    No

    BACKOFF_RETRY

    The retry policy for pushing messages to the endpoint when an error occurs.

    Valid values:

    • BACKOFF_RETRY: Backoff retry

    • EXPONENTIAL_DECAY_RETRY: Exponential decay retry

    For more information about retry policies, see NotifyStrategy.

    Default value: BACKOFF_RETRY.

    NotifyContentFormat

    Boolean

    No

    SIMPLIFIED

    The format of the message to be pushed to the endpoint.

    Valid values:

    • XML

    • JSON

    • SIMPLIFIED

    For more information about message formats, see NotifyContentFormat.

    Default value: XML.

    If you use Terraform to manage alicloud_message_service_subscription, specify the parameters as described in the following table.

    Parameter Name

    Type

    Required

    Example

    Description

    TopicName

    String

    Yes

    testTopic

    The name of the topic.

    SubscriptionName

    String

    Yes

    testSubscription

    The name of the subscription.

    PushType

    String

    Yes

    queue

    The type of endpoint.

    • http: HTTP push.

    • queue: Queue push.

    • mpush: Mobile Push.

    • alisms: Alibaba Cloud SMS push.

    • email: Email push.

    Endpoint

    String

    Yes

    demoQueue

    The endpoint address that receives messages for this subscription. If the endpoint is a queue, enter the queue name.

    MessageTag

    String

    No

    important

    The tag for message filtering in the subscription. Only messages with the same tag are pushed.

    The value must be a string of up to 16 characters.

    By default, messages are not filtered.

    NotifyStrategy

    Boolean

    No

    BACKOFF_RETRY

    The retry policy for pushing messages to the endpoint when an error occurs.

    Valid values:

    • BACKOFF_RETRY: Backoff retry

    • EXPONENTIAL_DECAY_RETRY: Exponential decay retry

    For more information about retry policies, see NotifyStrategy.

    Default value: BACKOFF_RETRY.

    NotifyContentFormat

    Boolean

    No

    SIMPLIFIED

    The format of the message to be pushed to the endpoint.

    Valid values:

    • XML

    • JSON

    • SIMPLIFIED

    For more information about message formats, see NotifyContentFormat.

    Default value: XML.

Response message

A response message consists of a status line, HTTP headers, and a message body:

  • HTTP status code

    HTTP/1.1 201

    Subscriptions for the same topic from the same subscriber must have unique names. If a subscription with the same name exists and has the same properties as the one you are creating, the HTTP status code 204 is returned. Otherwise, the HTTP status code 409 (Conflict) is returned.

  • Response-specific headers

    Parameter

    Type

    Example

    Description

    Location

    String

    http://$AccountId.mns.cn-hangzhou.aliyuncs.com/topics/$TopicName/subscriptions/$SubscriptionName

    The URL of the successfully created subscription. The format is `http://$AccountId.mns.{REGION}.aliyuncs.com/topics/$TopicName/subscriptions/$SubscriptionName`.

    For more information about other response headers, see Common parameters.

  • Response body

    None.

Examples

Request example:

PUT /topics/$TopicName/subscriptions/$SubscriptionName HTTP/1.1
Host: $AccountId.mns.cn-hangzhou.aliyuncs.com
Date: Tue, 08 Dec 2015 06:06:46 GMT
Authorization:MNS 15B4D3461F177624206A:xQE0diMbLRepdf3YB+FIEXA*****
x-mns-version: 2015-06-06

<?xml version="1.0" encoding="utf-8"?>
<Subscription xmlns="http://mns.aliyuncs.com/doc/v1/">
    <Endpoint>http://example.com</Endpoint>
    <NotifyStrategy>BACKOFF_RETRY</NotifyStrategy>
    <NotifyContentFormat>SIMPLIFIED</NotifyContentFormat>
    <FilterTag>important</FilterTag>
</Subscription>

Response example:

HTTP/1.1 201
x-mns-request-id:56667376B2B71C9C1600****
x-mns-version: 2015-06-06
Location: http://$AccountId.mns.cn-hangzhou.aliyuncs.com/topics/$TopicName/subscriptions/$SubscriptionName

Error codes

Error code

Error message

HTTP status code

Description

SubscriptionNameLengthError

Subscription name length is out of range, should be between 1 and 255.

400

The subscription name is invalid. Modify the name as prompted.

SubscriptionNameInvalid

The subscription you provided is invalid. SubscriptionName should start with alpha or digit, and contain only alpha, digit or -.

400

The subscription name is invalid. Modify the name as prompted.

SubscriptionAlreadyExist

The subscription you want to create already exists.

409

A subscription with the same name already exists, and its properties are different from those in the request.

EndpointInvalid

The endpoint you provided is invalid.

400

The Endpoint parameter is invalid. For more information, see Endpoint.

InvalidArgument

The length of filter tag should be between 1 and 16.

400

The length of the filter tag is invalid. Modify the length as prompted.