Clients can publish messages by making requests to the REST API using the HTTP 1.0 or 1.1 protocols. For the authentication and port mappings used by HTTP requests, see Protocols, port mappings, and authentication.
Note
HTTPS doesn't support a clientId
value like MQTT does.
clientId
is available when using MQTT, but it's not
available when using HTTPS.
HTTPS message URL
Devices and clients publish their messages by making POST requests to a client-specific endpoint and a topic-specific URL:
https://
IoT_data_endpoint
/topics/url_encoded_topic_name
?qos=1
-
IoT_data_endpoint
is the AWS IoT device data endpoint. You can find the endpoint in the AWS IoT console on the thing's details page or on the client by using the AWS CLI command:aws iot describe-endpoint --endpoint-type iot:Data-ATS
The endpoint should look something like this:
a3qjEXAMPLEffp-ats.iot.us-west-2.amazonaws.com
-
url_encoded_topic_name
is the full topic name of the message being sent.
HTTPS message code examples
These are some examples of how to send an HTTPS message to AWS IoT.
import requests
import argparse
# define command-line parameters
parser = argparse.ArgumentParser(description="Send messages through an HTTPS connection.")
parser.add_argument('--endpoint', required=True, help="Your AWS IoT data custom endpoint, not including a port. " +
"Ex: \"abcdEXAMPLExyz-ats.iot.us-east-1.amazonaws.com\"")
parser.add_argument('--cert', required=True, help="File path to your client certificate, in PEM format.")
parser.add_argument('--key', required=True, help="File path to your private key, in PEM format.")
parser.add_argument('--topic', required=True, default="test/topic", help="Topic to publish messages to.")
parser.add_argument('--message', default="Hello World!", help="Message to publish. " +
"Specify empty string to publish nothing.")
# parse and load command-line parameter values
args = parser.parse_args()
# create and format values for HTTPS request
publish_url = 'https://' + args.endpoint + ':8443/topics/' + args.topic + '?qos=1'
publish_msg = args.message.encode('utf-8')
# make request
publish = requests.request('POST',
publish_url,
data=publish_msg,
cert=[args.cert, args.key])
# print results
print("Response status: ", str(publish.status_code))
if publish.status_code == 200:
print("Response body:", publish.text)