Request
The Request Connector polls external HTTP(S) API endpoints on a configurable schedule, extracts device data from the responses, and forwards it to ThingsBoard as attributes and telemetry. It can also push data to external HTTP(S) APIs in response to shared attribute updates and RPC commands from ThingsBoard.
To enable this connector, add it to the connectors list in tb_gateway.json — see the
General Configuration reference.
Connector configuration: request.json
Section titled “Connector configuration: request.json”The connector reads its settings from a JSON file. Below is a full example:
{ "host": "http://127.0.0.1:5000", "SSLVerify": true, "security": { "type": "oauth2", "tokenUrl": "http://127.0.0.1:5000/oauth2/token", "clientId": "client_id", "clientSecret": "client_secret", "scope": "read write", "clientAuthentication": "header", "tokenPrefix": "Bearer", "tokenRefreshRatio": 0.75, "tokenRequestTimeout": 10, "tokenRetryDelay": 10, "defaultExpiresIn": 3600 }, "mapping": [ { "url": "getdata", "httpMethod": "GET", "httpHeaders": { "ACCEPT": "application/json" }, "data": { "name": "morpheus", "job": "leader" }, "allowRedirects": true, "timeout": 0.5, "scanPeriod": 5, "dataUnpackExpression": "", "converter": { "type": "json", "deviceNameJsonExpression": "SD8500", "deviceTypeJsonExpression": "SD", "attributes": [ { "key": "serialNumber", "type": "string", "value": "${serial}" } ], "telemetry": [ { "key": "Maintainer", "type": "string", "value": "${Developer}" }, { "key": "combine", "type": "string", "value": "${Developer}:${hum}" } ] } }, { "url": "/devices", "httpMethod": "GET", "httpHeaders": { "ACCEPT": "application/json" }, "allowRedirects": true, "timeout": 3.0, "scanPeriod": 1800, "subRequests": { "cumPwr": { "url": "/${id}/hist/values/ActiveEnergy/SUM13/3600?start=RELATIVE_-1HOUR&end=RELATIVE_-1HOUR&online=false&aggregate=false", "processingFunction": "def process_data(data, key): return {key: float(data[\"values\"][0][\"avg\"])/1000.0}" } }, "converter": { "type": "json", "deviceNameJsonExpression": "${id}-${type}", "deviceTypeJsonExpression": "GridVis", "attributes": [ { "key": "id", "type": "int", "value": "${id}" }, { "key": "type", "type": "string", "value": "${type}" }, { "key": "serialNr", "type": "string", "value": "${serialNr}" }, { "key": "name", "type": "string", "value": "${name}" }, { "key": "description", "type": "string", "value": "${description}" } ], "telemetry": [ { "key": "cumPwr", "type": "float", "value": "${cumPwr}" } ] } } ], "attributeUpdates": [ { "httpMethod": "POST", "httpHeaders": { "Content-Type": "application/json" }, "timeout": 0.5, "allowRedirects": true, "deviceNameFilter": "SD.*", "attributeFilter": "send_data", "requestUrlExpression": "sensor/${deviceName}/${attributeKey}", "requestValueExpression": "{\"${attributeKey}\":\"${attributeValue}\"}" } ], "serverSideRpc": [ { "deviceNameFilter": ".*", "methodFilter": "echo", "requestUrlExpression": "sensor/${deviceName}/request/${methodName}/${requestId}", "responseTimeout": 1, "httpMethod": "GET", "requestValueExpression": "${params}", "responseValueExpression": "${temp}", "timeout": 0.5, "httpHeaders": { "Content-Type": "application/json" } }, { "deviceNameFilter": ".*", "methodFilter": "no-reply", "requestUrlExpression": "sensor/${deviceName}/request/${methodName}/${requestId}", "httpMethod": "POST", "requestValueExpression": "${params}", "httpHeaders": { "Content-Type": "application/json" } } ]}Section “server”
Section titled “Section “server””| Parameter | Default | Description |
|---|---|---|
host |
http://127.0.0.1:5000 |
Base URL of the external HTTP(S) server |
SSLVerify |
true |
Verify the server’s SSL certificate |
Section “security”
Section titled “Section “security””Global authorization credentials sent with every request to the external server. Supported types: basic, oauth2,
and anonymous.
The connector sends an Authorization: Basic <base64(username:password)> header with every request.
| Parameter | Default | Description |
|---|---|---|
type |
basic |
Authorization type |
username |
username |
Username |
password |
password |
Password |
{ "security": { "type": "basic", "username": "username", "password": "password" }}Since ThingsBoard IoT Gateway version 3.8.3, environment variables can be specified for username and password fields. This allows you to avoid hardcoding sensitive information in the configuration file and provide it securely at runtime.
To use ENV variables for username and password, you can set them in your environment or define them in your docker-compose.yml file.
The following ENV variables are used for Basic authentication configuration:
REQUEST_BASIC_USERNAMEREQUEST_BASIC_PASSWORD
Note that if you specify environment variables for username and password, the values from the configuration file will be ignored, and the connector will use the values from the environment variables instead.
The connector obtains an OAuth 2.0 access token from the authorization server using the client credentials grant and
sends it in the Authorization: Bearer <token> header with every request. The token is renewed automatically before
it expires, so the connector keeps working with APIs that issue short-lived tokens.
| Parameter | Default | Description |
|---|---|---|
type |
oauth2 |
Authorization type |
tokenUrl |
URL for obtaining the access token. The token request uses the SSLVerify setting of the connector |
|
clientId |
Client ID from the API provider | |
clientSecret |
Client secret from the API provider | |
scope |
(Optional) Scopes to request, separated by spaces | |
clientAuthentication |
header |
(Optional) How the client ID and secret are sent to tokenUrl: header — in an Authorization: Basic header; body — as client_id and client_secret form fields |
tokenPrefix |
Bearer |
(Optional) Word placed before the token in the Authorization header. To send the token without a prefix, set it to "" |
tokenRefreshRatio |
0.75 |
(Optional) Part of the token lifetime after which the token is renewed, a number greater than 0 and up to 1 |
tokenRequestTimeout |
10 |
(Optional) Token request timeout in seconds |
tokenRetryDelay |
10 |
(Optional) Seconds to wait after a failed token request before the next attempt |
defaultExpiresIn |
(Optional) Token lifetime in seconds, used when the token response has no expires_in field |
{ "security": { "type": "oauth2", "tokenUrl": "http://127.0.0.1:5000/oauth2/token", "clientId": "client_id", "clientSecret": "client_secret", "scope": "read write" }}The clientId and clientSecret parameters support placeholders in the ${VARIABLE_NAME} format. This allows you to
keep the credentials out of the configuration file and provide them as environment variables according to your
system’s setup:
{ "security": { "type": "oauth2", "tokenUrl": "http://127.0.0.1:5000/oauth2/token", "clientId": "${API_CLIENT_ID}", "clientSecret": "${API_CLIENT_SECRET}" }}No authorization header is sent. Useful for public APIs or testing.
| Parameter | Default | Description |
|---|---|---|
type |
anonymous |
Authorization type |
{ "security": { "type": "anonymous" }}Section “mapping”
Section titled “Section “mapping””An array of endpoint configurations. Each entry defines a URL to poll, the HTTP method and headers, how often to poll, and how to convert the response into ThingsBoard device data.
| Parameter | Default | Description |
|---|---|---|
url |
getdata |
URL path appended to host for the request |
httpMethod |
GET |
HTTP method (GET, POST, etc.) |
httpHeaders |
{"ACCEPT": "application/json"} |
Additional HTTP headers |
allowRedirects |
true |
Follow HTTP redirects |
timeout |
0.5 |
Request timeout in seconds |
scanPeriod |
5 |
Polling interval in seconds |
data |
(Optional) Request body. An object is sent as form data (application/x-www-form-urlencoded); a string is sent as is. To send JSON, use a string and add "Content-Type": "application/json" to httpHeaders |
|
dataUnpackExpression |
(Optional) JSON-path expression that selects the part of the JSON response passed to the converter, for example ${payload}. If not set, the whole response is passed |
|
subRequests |
(Optional) Additional requests that fetch values for converter keys. See Subsection “subRequests” |
Subsection “converter”
Section titled “Subsection “converter””Defines how the HTTP response body is converted into ThingsBoard device data.
The default converter. Parses a JSON response body and maps fields to a device name, type, attributes, and telemetry.
| Parameter | Default | Description |
|---|---|---|
type |
json |
Converter type |
deviceNameJsonExpression |
SD8500 |
JSON expression to extract the device name; a plain string is used as-is |
deviceTypeJsonExpression |
SD |
JSON expression to extract the device type; a plain string is used as-is |
timeout |
60000 |
Milliseconds before a “Device Disconnected” event is triggered |
attributes[].type |
string |
Data type of the attribute value |
attributes[].key |
serialNumber |
Attribute key name in ThingsBoard |
attributes[].value |
${serial} |
JSON-path expression to extract the value from the response |
telemetry[].type |
string / integer |
Data type of the telemetry value |
telemetry[].key |
Maintainer |
Telemetry key name in ThingsBoard |
telemetry[].value |
${Developer} |
JSON-path expression to extract the value from the response |
telemetry[].tsField |
(Optional) JSON-path expression for a field carrying a datetime string used as the telemetry timestamp | |
telemetry[].dayfirst |
false |
(Optional) Treat the first number in tsField as the day (true) or month (false) |
telemetry[].yearfirst |
false |
(Optional) Treat the first number in tsField as the year |
Full mapping example:
{ "url": "getdata", "httpMethod": "GET", "httpHeaders": { "ACCEPT": "application/json" }, "allowRedirects": true, "timeout": 0.5, "scanPeriod": 5, "converter": { "type": "json", "deviceNameJsonExpression": "SD8500", "deviceTypeJsonExpression": "SD", "attributes": [ { "key": "serialNumber", "type": "string", "value": "${serial}" } ], "telemetry": [ { "key": "Maintainer", "type": "string", "value": "${Developer}" }, { "key": "Pressure", "type": "integer", "value": "${press}", "tsField": "${timestampField}", "dayfirst": true } ] }}You can also combine multiple response fields into a single value using expressions:
{ "key": "combine", "type": "string", "value": "${hum}:${temp}" }Use a custom converter when the response payload has a non-standard structure or requires
preprocessing. Pass any custom fields in extension-config — they are forwarded to the
converter class at initialization.
| Parameter | Description |
|---|---|
type |
Converter type, custom |
extension |
Name of your converter class |
extension-config |
Settings for your converter class; their structure is defined by the class |
{ "url": "getdata", "scanPeriod": 5, "converter": { "type": "custom", "extension": "CustomRequestUplinkConverter", "extension-config": { "key": "temperature" } }}Subsection “subRequests”
Section titled “Subsection “subRequests””Use sub-requests when a value for a converter key is available only from another endpoint. For each item of the
response, the connector sends one more request for every key of subRequests that matches an attributes[].key or
telemetry[].key of the converter. The result is added to the item before conversion, so the converter reads it like
any other field. Sub-requests use the same httpMethod, httpHeaders, data, timeout, and security as the main
request.
| Parameter | Description |
|---|---|
subRequests.<key>.url |
Sub-request URL. A value that starts with http is used as the full URL. Any other value is appended to the full URL of the main request. ${...} placeholders take values from the item, for example ${id} |
subRequests.<key>.processingFunction |
(Optional) Python code that defines process_data(data, key). Without it, the fields of the JSON response are added to the item as is, so the response must be a JSON object |
In the example below, the connector gets a list of devices from /devices. For each device, it requests
/devices/<id>/hist/values/..., divides the avg value from the response by 1000, and sends the result as the
cumPwr time series:
{ "url": "/devices", "httpMethod": "GET", "httpHeaders": { "ACCEPT": "application/json" }, "allowRedirects": true, "timeout": 3.0, "scanPeriod": 1800, "subRequests": { "cumPwr": { "url": "/${id}/hist/values/ActiveEnergy/SUM13/3600?start=RELATIVE_-1HOUR&end=RELATIVE_-1HOUR&online=false&aggregate=false", "processingFunction": "def process_data(data, key): return {key: float(data[\"values\"][0][\"avg\"])/1000.0}" } }, "converter": { "type": "json", "deviceNameJsonExpression": "${id}-${type}", "deviceTypeJsonExpression": "GridVis", "attributes": [ { "key": "id", "type": "int", "value": "${id}" } ], "telemetry": [ { "key": "cumPwr", "type": "float", "value": "${cumPwr}" } ] }}Section “attributeUpdates”
Section titled “Section “attributeUpdates””Optional. When a ThingsBoard shared attribute changes, the gateway sends an HTTP request to an external URL on the device.
| Parameter | Default | Description |
|---|---|---|
httpMethod |
GET |
HTTP method for the outgoing request |
httpHeaders |
{"CONTENT-TYPE": "application/json"} |
Additional HTTP headers |
timeout |
0.5 |
Request timeout in seconds |
allowRedirects |
true |
Follow HTTP redirects |
deviceNameFilter |
SD.* |
Regex filter on device name |
attributeFilter |
send_data |
Regex filter on attribute name |
requestUrlExpression |
sensor/${deviceName}/${attributeKey} |
Expression to build the request path appended to host |
requestValueExpression |
{"${attributeKey}":"${attributeValue}"} |
Expression to build the request body |
"attributeUpdates": [ { "httpMethod": "POST", "httpHeaders": { "CONTENT-TYPE": "application/json" }, "timeout": 0.5, "allowRedirects": true, "deviceNameFilter": "SD.*", "attributeFilter": "send_data", "requestUrlExpression": "sensor/${deviceName}/${attributeKey}", "requestValueExpression": "{\"${attributeKey}\":\"${attributeValue}\"}" }]Section “serverSideRpc”
Section titled “Section “serverSideRpc””Optional. Maps ThingsBoard RPC commands to outgoing HTTP requests sent to the device.
| Parameter | Default | Description |
|---|---|---|
deviceNameFilter |
.* |
Regex filter on device name |
methodFilter |
echo |
Regex filter on RPC method name |
requestUrlExpression |
sensor/${deviceName}/request/${methodName}/${requestId} |
Expression to build the request path appended to host |
responseTimeout |
1 |
Seconds to wait for a response |
httpMethod |
GET |
HTTP method for the outgoing request |
requestValueExpression |
${params} |
Expression to build the request body |
responseValueExpression |
(Optional) Expression that extracts the value to return from a JSON response, for example ${temp}. Without it, the whole response body is returned |
|
timeout |
0.5 |
Request timeout in seconds |
httpHeaders |
{"CONTENT-TYPE": "application/json"} |
Additional HTTP headers |
The gateway always waits for the HTTP response and replies to ThingsBoard: {"result": <response body>} on success
or {"error": "<message>"} on failure.
"serverSideRpc": [ { "deviceNameFilter": ".*", "methodFilter": "echo", "requestUrlExpression": "sensor/${deviceName}/request/${methodName}/${requestId}", "responseTimeout": 1, "httpMethod": "GET", "requestValueExpression": "${params}", "timeout": 0.5, "httpHeaders": { "Content-Type": "application/json" } }, { "deviceNameFilter": ".*", "methodFilter": "no-reply", "requestUrlExpression": "sensor/${deviceName}/request/${methodName}/${requestId}", "httpMethod": "POST", "requestValueExpression": "${params.hum}", "httpHeaders": { "Content-Type": "application/json" } }]The connector also has built-in get and set RPC methods that send an HTTP request without configuration. See the
Reserved RPC methods guide for details.
Was this helpful?