使用Subscribe接口来创建Subscription。

描述

该接口用于订阅主题,创建Subscription。Subscription名称是一个不超过255个字符的字符串,必须以字母或者数字为首字符,剩余部分可以包含字母、数字和短划线(-)。

创建Subscription时,需要指定对应的Endpoint,否则不合法。

Request

请求消息由以下几部分组成:

  • 请求行

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

  • 特有Request Header

    无,请参见公共参数

  • Request Body

    Request Body为XML格式,XML总包含创建Subscription的属性。

    参数名称 是否必选 说明 参数值
    Endpoint 描述此次订阅中接收消息的终端地址。

    目前四种Endpoint:

    • HttpEndpoint:必须以http://为前缀。
    • QueueEndpoint:格式为acs:mns:{REGION}:{AccountID}:queues/{QueueName}。
    • MailEndpoint:格式为mail:directmail:{MailAddress}。
    • SmsEndpoint:格式为sms:directsms:anonymous 或sms:directsms:{Phone}。
    FilterTag 描述了该订阅中消息过滤的标签(标签一致的消息才会被推送)。 不超过16个字符的字符串,默认不进行消息过滤。
    NotifyStrategy 描述了向Endpoint推送消息出现错误时的重试策略。 BACKOFF_RETRY或者 EXPONENTIAL_DECAY_RETRY,默认为BACKOFF_RETRY。重试策略的具体描述,请参见NotifyStrategy
    NotifyContentFormat 描述了向Endpoint推送的消息格式。 XML、JSON或者SIMPLIFIED,默认为XML。消息格式的具体描述,请参见NotifyContentFormat

Response

返回消息由返回状态行、HTTP头和消息体三部分组成:

  • HTTP Status Code

    HTTP/1.1 201

    同一个订阅者在同一个主题下的Subscription不能重名,当出现重名时,如果正在创建的Subscription已经存在的Subscription属性完全相同,返回204;否则返回409 (Conflict)。

  • 特有Response Header
    参数名称 说明
    Location 返回创建成功的SubscriptionURL,格式如下:http://$AccountId.mns.cn-hangzhou.aliyuncs.com/topics/$TopicName/subscriptions/$SubscriptionName。

    其它Response Header,请参见公共参数

  • Response Body

    无。

示例

请求示例:

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+****AMPLE=
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>

返回示例:

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

Special Error

错误代码 错误信息 状态码
SubscriptionNameLengthError Subscription name length is out of range, should be between 1 and 255. 400
SubscriptionNameInvalid The subscription you provided is invalid. SubscriptionName should start with alpha or digit, and contain only alpha, digit or -. 400
SubscriptionAlreadyExist The subscription you want to create already exists. 409
EndpointInvalid The endpoint you provided is invalid. 400
InvalidArgument The length of filter tag should be between 1 and 16. 400