HTTP/REST
The HTTP/REST Module is suitable for either of the following methods:
manual polling of endpoints (read)
continuous polling of endpoints (subscribe)
publishing data to endpoints (write)
The HTTP server to be contacted is described by the Cybus::Connection resource. This resource defines hostname, scheme, port, but also additional headers or authentication info. Each REST endpoint is described by a Cybus::Endpoint resource (no pun intended) that defines access to a particular REST path, optionally specifying additional query parameters or headers for this REST path.
The currently supported authentication methods are
Username/password for a basicAuth scheme
Arbitrary header for e.g. literal Bearer Tokens
Oauth 2.0 Client Credentials Grant
Commissioning File Specifics
The Endpoints for the HTTP connections may either use subscribe
for subscribing, read
for event-driven polling, or write
for publishing, as described above.
Subscribing Data from a REST Server
When subscribing, the REST Server is polled at regular intervals according to the interval
property of the endpoint resource. Each endpoint must contain a path and may contain additional header and query parameters.
If the request fails, a warning is logged but no action is taken.
Polling is implemented with a best-effort strategy, which means polling will take place at best with the desired interval. If the REST server response takes longer than the polling interval, the next request will not be triggered before the previous has been completed. This prevents building up of infinite queues in the event of unfortunate response time / polling interval combinations. However, this serialization is implemented per endpoint (per REST path), so that multiple requests to different paths can actually be in-flight in parallel.
Response Message
Similar to most protocol implementations in the Cybus Connectware, the server response will be wrapped in a JSON structure that follow the convention timestamp/value, like so:
If the server responds with a Content-type=application/json, the response JSON will be embedded to this object as value of the "value"
property, like so:
If the server response is not parseable as JSON, it will be embedded into the message object as a string, hence
Reading Data from a REST Server
Data can also be read from the server on certain events. Reading data from the server is triggered by sending a message to the /req
topic of the endpoint (see also Operation results). The response from the server is written to the /res
topic of the endpoint.
The message sent on the endpoint’s /req
topic does not need to have any content to trigger reading of data.
Dynamic path
If the REST endpoint to be called requires a dynamic (changing) path, the configured path property can be overwritten by the message to the endpoint. This is done by providing a path
property string in the message payload.
Example message with dynamic path overwriting (to be sent on the /req
topic):
It is also possible to append some path suffix to the configured path property by the message to the endpoint. Appending requires providing a pathAppend
property string in the message payload.
Example message with dynamic path appended using pathAppend
(to be sent on the /req
topic):
Dynamic query parameters
If the REST endpoint to be called requires dynamic query parameters, these can be provided in the message payload as key-value pairs.
Example message with dynamic query parameters (to be sent on the /req
topic):
If the endpoint’s configuration already has a query
property, both provided query parameters are merged. If any parameters are duplicated, the dynamic parameters from the message payload override the ones from the static configuration.
Publishing Data to a REST Server
The HTTP/REST Protocol Mapper also supports the other direction of data flow, namely publishing (pushing) data from MQTT to a REST server using the write
property in the Cybus::Endpoint resource. Publishing does not work on a regular time interval but will directly forward any new message that is received from the broker on the /set
topic of the endpoint.
The data of the internal MQTT message must be of type object and has to contain a property called body
, which will be forwarded as the body of the HTTP request.
The request mime type header of the outgoing HTTP request will be set according to the internal MQTT message value: If the message contains parseable JSON, the mime type of the HTTP request will be set to application/json, otherwise it will be set to application/octet-stream. Besides that, no further changes are performed on the request. Any additional headers as defined in the endpoint resource will be passed on as given.
The result of the HTTP request is returned on the /res
topic of the endpoint, as explained here: Operation results. The data message on the result topic will have the following format:
In this result message, id
is the request identifier that was sent on the original request, timestamp
is the Unix timestamp of when the result was received, and result
is the JSON object with the actual result, but its content depends on the concrete protocol implementation.
If there was an error, the resulting JSON object does not contain a property result but instead a property error
. This property is simply a string variable and contains an explanatory message of the error. Hence, in the error case the data message on the result topic will have the following format:
Dynamic path
If the REST endpoint to be called requires a dynamic (changing) path, the configured path property can be overwritten by the message to the endpoint. This is done by providing a path
property string in the message payload.
Example message with dynamic path overwriting (to be sent on the /set
topic):
It is also possible to append some path suffix to the configured path property by the message to the endpoint. Appending requires providing a pathAppend
property string in the message payload.
Example message with dynamic path appended using pathAppend
(to be sent on the /set
topic):
Encoded request body
If it is required to send binary data as the request body, it is possible to specify the encoding of the message payload body parameter using a flag called bufferFromBody
. The contend of the body parameter will then be converted into a buffer, respecting the encoding. Allowed encodings can be found here https://nodejs.org/api/buffer.html#buffers-and-character-encodings.
Connection Probing
While HTTP by itself doesn’t require a continuous connection, the HTTP protocol implementation in the Connectware is monitoring the connection state for up-to-date connectivity information. The connection state is estimated by monitoring each successful read and write, and additionally by a probing function scheduled to run if no read or write operation is in progress. The additional probing function is run after the time interval from the property probeInterval
(default: 10 seconds).
By default the probing is performed by sending an OPTIONS request against the endpoint server. If this request fails with network error (e.g. ECONNRESET, ETIMEOUT, EUNREACHABLE), the connection state is assumed to be disconnected. If it succeeds, or if it at least gets a HTTP response from the endpoint, the connection is assumed to be connected.
Additionally the properties probePath
and probeMethod
allow configuring a custom HTTP path and HTTP method to be used by the probing function. See the section Connection Properties for more details on these properties.
Oauth 2.0 Client Credentials Grant
This module supports the Oauth 2.0 Client Credentials Grant flow for authenticating requests (see rfc6749 Section-4.4).
Tokens are automatically refreshed when they expire.
To use this authentication method the property oauthClientCredentials needs to be configured in the connection object providing client_id, client_secret, auth_url and, optionally, as some Oauth 2.0 implementations do not require it, audience and grant_type (which defaults to client_credentials).
Example configuration:
Service Commissioning File Example
Last updated