HTTP specification¶
The HTTP protocol of the Messaging API is ideal for short-lived connections between external clients (HTTP clients) and the DSH. The HTTP client can only receive retained messages from the DSH. This supports the scenario of devices that are in sleeping mode most of the time, wake up at a set time interval, make a connection, exchange data with the DSH, and go back to sleep again.
The Messaging API has the following endpoints and methods for HTTP:
| Endpoint | Methods | Description |
|---|---|---|
data/v0/single |
POST, GET, DELETE | Act on the data of a single MQTT topic |
data/v0/multi |
POST | Fetch retained data from multiple MQTT topics |
Through these endpoints, the HTTP has equivalent functionality to MQTT:
- Publish a single value to an MQTT topic.
- Subscribe to one or multiple MQTT topics. However, when you send a GET request, you can only query the Latest Value Store for retained data.
- Unlike MQTT, HTTP doesn’t offer live updates of the data as it appears on the topics.
Tip
See Retained messages for more information.
data/v0/single¶
Use this endpoint to act on the data of a single MQTT topic.
POST¶
Use the “POST” method to publish data to an MQTT topic.
Request¶
You can use the following parameters in the request:
| Parameter | Description | Default value |
|---|---|---|
| qos |
The Quality of Service (QoS) defines the guarantee of delivery for a specific message. The concept is taken from MQTT. The Messaging API supports two levels for HTTP:
|
1 |
| retained |
Whether to mark the message as "retained":
|
true
|
Response¶
The DSH returns the following statuses:
| Status | Description |
|---|---|
| 200 | The operation was successful. |
| 400 | The HTTP request has invalid properties. |
| 401 | The authorization header is missing or wrong. |
| 403 | The client isn't authorized to publish to the specified MQTT topic. |
| 404 | The MQTT topic path isn't supported: invalid MQTT topic layout or unknown DSH stream. |
| 405 | The method isn't supported: the Messaging API only supports POST, GET and DELETE for HTTP. |
| 408 | No request received in the first 10 seconds after the connection setup. |
| 411 | The content-length header is missing. |
| 415 | The media type isn't supported: use text/plain, base64, or application/octet-stream.
|
| 500 | The service for the Messaging API is down. |
| 505 | The HTTP version isn't supported: use HTTP 1.1. |
Example¶
POST /data/v0/single/tt/weather/1/2/0/2/0/0/3/3/1/3/3/2/0/0/0/1 HTTP/1.1
User-Agent: <user-agent>
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6Ik1ZIE1RVFQgVE9LRU4ifQ.U0LWGERXajMqmXRbsYtLnlT26rqU4gZMQaU1Z_qmV7Q
Content-Type: application/json
Content-Length: 293
Connection: close
{
"sunrise": 1617167648,
"code": 800,
"snow_3h": 0,
"rain_3h": 0,
"shortdesc": "Clear",
"icon": "http://openweathermap.org/img/w/01d.png",
"description": "clear sky",
"winddir": 292,
"pressure": 1021,
"cloudcover": 0,
"sunset": 1617214214,
"temperature": 288.93,
"humidity": 60,
"windspeed": 0.45,
"timestamp": 1617182232
}
GET¶
Use the “GET” method to fetch retained data from an MQTT topic.
Request¶
In the “Accept” header, you can specify which data format the DSH should use in the response:
Accept: text/plainAccept: base64Accept: application/octet-stream
Response¶
The DSH returns the following statuses:
| Status | Description |
|---|---|
| 200 | The operation was successful. |
| 400 | The HTTP request has invalid properties. |
| 401 | The authorization header is missing or wrong. |
| 403 | The client isn't authorized to subscribe to the specified MQTT topic. |
| 404 | The MQTT topic path isn't supported: no retained data, invalid MQTT topic layout, or unknown DSH stream. |
| 405 | The method isn't supported: the Messaging API only supports POST, GET and DELETE for HTTP. |
| 408 | No request received in the first 10 seconds after the connection setup. |
| 500 | The service for the Messaging API is down. |
| 505 | The HTTP version isn't supported: use HTTP 1.1. |
Example¶
HTTP/1.1 200 OK
Content-Type: application/json
Connection: Closed
Content-Length: 293
{
"sunrise": 1617167648,
"code": 800,
"snow_3h": 0,
"rain_3h": 0,
"shortdesc": "Clear",
"icon": "http://openweathermap.org/img/w/01d.png",
"description": "clear sky",
"winddir": 292,
"pressure": 1021,
"cloudcover": 0,
"sunset": 1617214214,
"temperature": 288.93,
"humidity": 60,
"windspeed": 0.45,
"timestamp": 1617182232
}
DELETE¶
Use the “DELETE” method to remove a retained message from the specified MQTT topic.
Request¶
Because of the MQTT specification, removing a message will propagate to all MQTT clients that have active subscriptions on that MQTT topic. They will receive the “delete” message.
Response¶
The DSH returns the following statuses:
| Status | Description |
|---|---|
| 200 | The operation was successful. |
| 400 | The HTTP request has invalid properties. |
| 401 | The authorization header is missing or wrong. |
| 403 | The client isn't authorized to publish to the specified MQTT topic. |
| 404 | The MQTT topic path isn't supported: invalid MQTT topic layout or unknown DSH stream. |
| 405 | The method isn't supported: the Messaging API only supports POST, GET and DELETE for HTTP. |
| 408 | No request received in the first 10 seconds after the connection setup. |
| 500 | The service for the Messaging API is down. |
| 505 | The HTTP version isn't supported: use HTTP 1.1. |
Example¶
data/v0/multi¶
Use this endpoint to fetch retained data from the Latest Value Store. This supports subscriptions to multiple MQTT topics in one single request. In other words, you can use:
- Multiple topic filters
- MQTT wildcard topics (
+and#)
POST¶
Use the “POST” method to fetch the retained data for multiple MQTT topics.
Request¶
The HTTP content-type header of the request must be “application/json”, and the JSON body must have the following schema:
{
"content-type": "text/plain" | "base64",
"topic-filters": [ "<topic-filter>", "<topic-filter>", … ]
}
- The
content-typeinside the JSON body specifies how the Messaging API needs to deliver the data. Options are:"content-type": "text/plain""content-type": "base64"
- The
topic-filterscan be a list of any valid MQTT-style topic filters, including wildcard characters (+and#).
Response¶
The DSH returns the following statuses:
| Status | Description |
|---|---|
| 200 | The operation was successful. |
| 400 | The HTTP request has invalid properties, or malformed JSON body. |
| 401 | The authorization header is missing or wrong. |
| 403 | The client isn't authorized to subscribe to the specified MQTT topic. |
| 404 | The MQTT topic path isn't supported: invalid MQTT topic layout or unknown DSH stream. |
| 405 | The method isn't supported: the Messaging API only supports POST for this endpoint. |
| 408 | No request received in the first 10 seconds after the connection setup. |
| 411 | The content-length header is missing. |
| 415 | The media type isn't supported: use text/plain or base64.
|
| 500 | The service for the Messaging API is down. |
| 505 | The HTTP version isn't supported: use HTTP 1.1. |
Example¶
POST /data/v0/multi HTTP/1.1
User-Agent: <user-agent>
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6Ik1ZIE1RVFQgVE9LRU4ifQ.U0LWGERXajMqmXRbsYtLnlT26rqU4gZMQaU1Z_qmV7Q
Content-Type: application/json
Content-Length: 171
Connection: close
{
"content-type": "text/plain",
"topic-filters": [
"/tt/weather/1/2/0/2/0/0/3/3/1/3/3/2/#",
"/tt/weather/1/2/0/2/0/0/3/3/1/3/3/1/#",
"/tt/weather/1/2/0/2/0/0/3/3/1/3/3/0/#"
]
}
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: . . .
Connection: Closed
[
{
"topic": "/tt/weather/1/2/0/2/0/0/3/3/1/3/3/2/0/0/0/1",
"payload" : "{ \"sunrise\": 1617167648, \"code\": 250, \"snow_3h\": 0, \"rain_3h\": 0, . . ."
},
{
"topic": "/tt/weather/1/2/0/2/0/0/3/3/1/3/3/2/0/0/1/1",
"payload" : "{ \"sunrise\": 1617167648, \"code\": 135, \"snow_3h\": 0, \"rain_3h\": 0, . . ."
},
{
"topic": "/tt/weather/1/2/0/2/0/0/3/3/1/3/3/1/0/0/0/0",
"payload" : "{ \"sunrise\": 1617167648, \"code\": 765, \"snow_3h\": 0, \"rain_3h\": 0, . . ."
},
…
]