You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -40,26 +40,27 @@ The payload for method requests and responses is a JSON document up to 128 KB.
40
40
41
41
## Invoke a direct method from a back-end app
42
42
43
-
Now, invoke a direct method from a back-end app.
43
+
To invoke a direct method from a back-end app use the [Invoke device method](/rest/api/iothub/service/devices/invoke-method) REST API or its equivalent in one of the [IoT Hub service SDKs](iot-hub-devguide-sdks.md#azure-iot-hub-service-sdks).
44
44
45
45
### Method invocation
46
46
47
47
Direct method invocations on a device are HTTPS calls that are made up of the following items:
48
48
49
-
* The *request URI* specific to the device along with the [API version](/rest/api/iothub/service/devices/invokemethod):
49
+
* The *request URI* specific to the device along with the API version:
* *Headers* that contain the authorization, request ID, content type, and content encoding.
57
+
* *Headers* that contain the authorization, content type, and content encoding.
58
58
59
59
* A transparent JSON *body* in the following format:
60
60
61
61
```json
62
62
{
63
+
"connectTimeoutInSeconds": 200,
63
64
"methodName": "reboot",
64
65
"responseTimeoutInSeconds": 200,
65
66
"payload": {
@@ -75,7 +76,7 @@ The value provided as `connectTimeoutInSeconds` in the request is the amount of
75
76
76
77
#### Example
77
78
78
-
This example will allow you to securely initiate a request to invoke a Direct Method on an IoT device registered to an Azure IoT Hub.
79
+
This example will allow you to securely initiate a request to invoke a direct method on an IoT device registered to an Azure IoT hub.
79
80
80
81
To begin, use the [Microsoft Azure IoT extension for Azure CLI](https://github.com/Azure/azure-iot-cli-extension) to create a SharedAccessSignature.
81
82
@@ -100,14 +101,15 @@ curl -X POST \
100
101
}'
101
102
```
102
103
103
-
Execute the modified command to invoke the specified Direct Method. Successful requests will return an HTTP 200 status code.
104
+
Execute the modified command to invoke the specified direct method. Successful requests will return an HTTP 200 status code.
104
105
105
106
> [!NOTE]
106
-
> The above example demonstrates invoking a Direct Method on a device. If you wish to invoke a Direct Method in an IoT Edge Module, you would need to modify the url request as shown below:
107
+
> The example above demonstrates invoking a direct method on a device. If you want to invoke a direct method in an IoT Edge Module, you would need to modify the url request as shown below:
The back-end app receives a response that is made up of the following items:
@@ -117,7 +119,7 @@ The back-end app receives a response that is made up of the following items:
117
119
* 404 indicates that either device ID is invalid, or that the device was not online upon invocation of a direct method and for`connectTimeoutInSeconds` thereafter (use accompanied error message to understand the root cause);
118
120
* 504 indicates gateway timeout caused by device not responding to a direct method call within `responseTimeoutInSeconds`.
119
121
120
-
**Headers* that contain the ETag, request ID, content type, and content encoding.
122
+
**Headers* that contain the request ID, content type, and content encoding.
121
123
122
124
* A JSON *body*in the following format:
123
125
@@ -128,25 +130,25 @@ The back-end app receives a response that is made up of the following items:
128
130
}
129
131
```
130
132
131
-
Both `status` and `body` are provided by the device and used to respond with the device's own status code and/or description.
133
+
Both `status` and `payload` are provided by the device and used to respond with the device's own status code and the method response.
132
134
133
135
### Method invocation for IoT Edge modules
134
136
135
-
Invoking direct methods using a module ID is supported in the [IoT Service Client C# SDK](https://www.nuget.org/packages/Microsoft.Azure.Devices/).
137
+
Invoking direct methods on a module is supported by the [Invoke module method](/rest/api/iothub/service/modules/invoke-method) REST API or its equivalent in one of the IoT Hub service SDKs.
136
138
137
-
For this purpose, use the `ServiceClient.InvokeDeviceMethodAsync()` method and pass in the `deviceId` and `moduleId` as parameters.
139
+
The `moduleId` is passed along with the `deviceId` in the request URI when using the REST API or as a parameter when using a service SDK. For example, `https://<iothubName>.azure-devices.net/twins/<deviceId>/modules/<moduleName>/methods?api-version=2021-04-12`. The request body and response is similar to that of direct methods invoked on the device.
138
140
139
141
## Handle a direct method on a device
140
142
141
-
Let's look at how to handle a direct method on an IoT device.
143
+
On an IoT device, direct methods can be received over MQTT, AMQP, or either of these protocols over WebSockets. The [IoT Hub device SDKs](iot-hub-devguide-sdks.md#azure-iot-hub-device-sdks) help you receive and respond to direct methods on devices without having to worry about the underlying protocol details.
142
144
143
145
### MQTT
144
146
145
-
The following section is for the MQTT protocol.
147
+
The following section is for the MQTT protocol. To learn more about using the MQTT protocol directly with IoT Hub, see [MQTT protocol support](iot-hub-mqtt-support.md).
146
148
147
149
#### Method invocation
148
150
149
-
Devices receive direct method requests on the MQTT topic: `$iothub/methods/POST/{method name}/?$rid={request id}`. The number of subscriptions per device is limited to 5. It is therefore recommended not to subscribe to each direct method individually. Instead consider subscribing to `$iothub/methods/POST/#` and then filter the delivered messages based on your desired method names.
151
+
Devices receive direct method requests on the MQTT topic: `$iothub/methods/POST/{method name}/?$rid={request id}`. However, the `request id` is generated by IoT Hub and cannot be known ahead of time, so subscribe to `$iothub/methods/POST/#` and then filter the delivered messages based on method names supported by your device. (You'll use the `request id` to respond.)
150
152
151
153
The body that the device receives is in the following format:
152
154
@@ -171,7 +173,7 @@ The body is set by the device and can be any status.
171
173
172
174
### AMQP
173
175
174
-
The following section is for the AMQP protocol.
176
+
The following section is for the AMQP protocol. To learn more about using the AMQP protocol directly with IoT Hub, see [AMQP protocol support](iot-hub-amqp-support.md).
0 commit comments