概述
消息队列 MQTT 版支持客户端通过 HTTP(s) POST 方式发布 MQTT 消息。
该方式支持 HTTP 1.0和1.1协议
每成功调用一次本接口,都会计算为一次消息TPS,从而影响您的计费,具体可参见 价格说明
使用方法
URL 示例
http(s)://${instance-access-point}/topics/${url-encoded-topic-name}?qos=${qos}&retained=${retained}&client_id=${ClientId}&username=${Username}
instance-access-point、qos、retained、client_id、username 等字段,具体定义见下文 请求参数 。
认证方式
目前支持 Basic HTTP Authentication 和基于 X509 证书的“一机一证”两种认证方式,暂未支持基于 JWT 的认证方式。
Basic HTTP Authentication
进行基本认证过程中的参数如下:
请求的HTTP头字段会包含Authorization字段,形式如下:
Authorization: Basic <credentials><credentials> = username:password 的 Base64 编码。username 会参与 ACL 授权。BYOC(基于 X.509 证书认证)
客户端信任的根证书:CA.crt
客户端证书链文件:client.chain.crt
客户端私钥文件:client.pkcs8.key
请求参数
参数 | 语义 | 示例值 | 可选 |
instance-access-point | mqtt-example-sh-public.mqtt.tencenttdmq.com | 否 | |
url-encoded-topic-name | 要发送的 Topic 名称,URL encoded。 | home | 否 |
QoS | 发送 MQTT 消息时选择 QoS,默认值为1,即 at-least-once。 | 1 | 是 |
Retained | 发送的消息是否是 retained 消息。 | false | 否 |
client_id | 使用 client-id 发送 MQTT 消息,参与授权验证。 如果在一机一证场景,使用证书 Common Name 字段。 其它场景默认值是 http-${connection-id}。 | curl001 | 是 |
username | 连接、发送时使用的用户名,参与授权验证。 如果使用 Basic Authentication Header,取 Basic Authentication Header 中的用户名部分。 | SampleUser | 是 |
ResponseTopic | sample/response/topic | 是 | |
CorrelationData | SN0001 | 是 | |
ContentType | application/json | 是 | |
PayloadFormatIndicator | 1 | 是 | |
MessageExpiryInterval | 3600 | 是 | |
UserProp.N.Key | sampleKey | 是 | |
UserProp.N.Value | sampleValue | 是 |
请求示例
示例 1
使用 Basic Authentication(缺省 username )
curl --verbose --request POST -H "Authorization: Basic dXNlcjA6c2VjcmV0MA==" --data "{ \\"message\\": \\"Hello, world\\" }" "http://mqtt.cloud.tencent.com/topics/home?qos=1&client_id=curl-x&retained=true"
示例 2
使用 Basic Authentication(指定 username)
curl --verbose -k --request POST -H "Authorization: Basic dXNlcjA6c2VjcmV0MA==" --data "{ \\"message\\": \\"Hello, world\\" }" "https://mqtt.cloud.tencent.com/topics/home?qos=1&client_id=curl-x&retained=true&username=sample-user"
示例3
使用 BYOC 证书认证
curl --tlsv1.2 \\--cacert CA.crt \\--cert client.chain.crt \\--key client.pkcs8.key \\--verbose \\--request POST \\--data "{ \\"message\\": \\"Hello, world\\" }" \\"https://mqtt.cloud.tencent.com/topics/home?qos=1"
授权
HTTP POST 方式与 WebSocket 类似,需经过 ACL 授权流程。如果使用 HTTP 1.0协议,每次发送都会执行完整的 CONNECT,PUBLISH 流程,也会执行对应的鉴权、授权操作。