Publish - AWS IoT

Publish

Publishes an MQTT message.

Requires permission to access the Publish action.

For more information about MQTT messages, see MQTT Protocol in the AWS IoT Developer Guide.

For more information about messaging costs, see AWS IoT Core pricing - Messaging.

Request Syntax

POST /topics/topic?contentType=contentType&messageExpiry=messageExpiry&qos=qos&responseTopic=responseTopic&retain=retain HTTP/1.1 x-amz-mqtt5-user-properties: userProperties x-amz-mqtt5-payload-format-indicator: payloadFormatIndicator x-amz-mqtt5-correlation-data: correlationData payload

URI Request Parameters

The request uses the following URI parameters.

contentType

A UTF-8 encoded string that describes the content of the publishing message.

correlationData

The base64-encoded binary data used by the sender of the request message to identify which request the response message is for when it's received. correlationData is an HTTP header value in the API.

messageExpiry

A user-defined integer value that represents the message expiry interval in seconds. If absent, the message doesn't expire. For more information about the limits of messageExpiry, see AWS IoT Core message broker and protocol limits and quotas from the AWS Reference Guide.

payloadFormatIndicator

An Enum string value that indicates whether the payload is formatted as UTF-8. payloadFormatIndicator is an HTTP header value in the API.

Valid Values: UNSPECIFIED_BYTES | UTF8_DATA

qos

The Quality of Service (QoS) level. The default QoS level is 0.

Valid Range: Minimum value of 0. Maximum value of 1.

responseTopic

A UTF-8 encoded string that's used as the topic name for a response message. The response topic is used to describe the topic which the receiver should publish to as part of the request-response flow. The topic must not contain wildcard characters.

retain

A Boolean value that determines whether to set the RETAIN flag when the message is published.

Setting the RETAIN flag causes the message to be retained and sent to new subscribers to the topic.

Valid values: true | false

Default value: false

topic

The name of the MQTT topic.

Required: Yes

userProperties

A JSON string that contains an array of JSON objects. If you don’t use AWS SDK or AWS CLI, you must encode the JSON string to base64 format before adding it to the HTTP header. userProperties is an HTTP header value in the API.

The following example userProperties parameter is a JSON string which represents two User Properties. Note that it needs to be base64-encoded:

[{"deviceName": "alpha"}, {"deviceCnt": "45"}]

Request Body

The request accepts the following binary data.

payload

The message body. MQTT accepts text, binary, and empty (null) message payloads.

Publishing an empty (null) payload with retain = true deletes the retained message identified by topic from AWS IoT Core.

Response Syntax

HTTP/1.1 200

Response Elements

If the action is successful, the service sends back an HTTP 200 response with an empty HTTP body.

Errors

InternalFailureException

An unexpected error has occurred.

HTTP Status Code: 500

InvalidRequestException

The request is not valid.

HTTP Status Code: 400

MethodNotAllowedException

The specified combination of HTTP verb and URI is not supported.

HTTP Status Code: 405

ThrottlingException

The rate exceeds the limit.

HTTP Status Code: 429

UnauthorizedException

You are not authorized to perform this operation.

HTTP Status Code: 401

See Also

For more information about using this API in one of the language-specific AWS SDKs, see the following: