Call this operation to publish a message to a specified topic.

Request parameters

Parameter Type Required Description
Action String Yes The operation that you want to perform. Set the value to Pub.
ProductKey String Yes The ProductKey of the device that publishes this message.
TopicFullName String Yes

The topic that receives messages, such as /a1Q5XoY****/device1/user/update.

Note
  • System topics are not supported.
  • The specified topic must have the Publish or Publish and Subscribe permissions.

You can call QueryProductTopic to query the topic categories of the product. You can also go to the Topic List tab page of the Device Details page to view the topics of the device.

MessageContent String Yes The body of the message to be published. To generate the message body, you must convert the raw message into binary data and perform Base64 encoding.
Qos Integer No

Specifies the QoS of message sending. Valid values:

0: Indicates that the message is published at most one time.

1: Indicates that the message is published at least one time.

If you do not specify this parameter, the value is 0.

Note QoS=1 messages can be stored in IoT Platform for up to 7 days. IoT Platform does not store QoS=0 messages.
Common request parameters N/A Yes See Common parameters.

Response parameters

Parameter Type Description
RequestId String The GUID generated by Alibaba Cloud for the request.
Success Boolean Indicates whether the call is successful. A value of true indicates that the call is successful. A value of false indicates that the call has failed.
ErrorMessage String The error message returned when the call fails.
Code String The error code returned when the call fails. For more information about error codes, see Error codes.
MessageId String The message ID generated by IoT Platform when the message is sent.

Examples

Sample requests

https://iot.cn-shanghai.aliyuncs.com/?Action=Pub
&ProductKey=a1Q5XoY****
&TopicFullName=%2a1Q5XoY****%2Fdevice1%2Fuser%2Fget
&MessageContent=aGVsbG8gd29ybGQ%3D 
&Qos=0 
&Common request parameters

Sample responses

  • JSON format

    {
          "RequestId":"BB71E443-4447-4024-A000-EDE09922891E",
          "Success":true,
          "MessageId":889455942124347329
      }
  • XML format

    <? xml version="1.0" encoding="UTF-8"? > 
       <PubResponse>
          <RequestId>BB71E443-4447-4024-A000-EDE09922891E</RequestId>
          <Success>true</Success>
          <MessageId>889455942124347329</MessageId>
      </PubResponse>