Skip to content

Commit e081ef2

Browse files
authored
Merge pull request #204528 from JimacoMS4/fix-feedback-94680-request-id-for-method-invocation
Fix feedback 94680 direct methods request ID
2 parents 6a54fc3 + 228d914 commit e081ef2

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

articles/iot-hub/iot-hub-devguide-direct-methods.md

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ author: kgremban
55
ms.service: iot-hub
66
services: iot-hub
77
ms.topic: conceptual
8-
ms.date: 07/17/2018
8+
ms.date: 07/15/2022
99
ms.author: kgremban
1010
ms.custom: [amqp, mqtt,'Role: Cloud Development', 'Role: IoT Device']
1111
---
@@ -40,26 +40,27 @@ The payload for method requests and responses is a JSON document up to 128 KB.
4040

4141
## Invoke a direct method from a back-end app
4242

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).
4444

4545
### Method invocation
4646

4747
Direct method invocations on a device are HTTPS calls that are made up of the following items:
4848

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:
5050

5151
```http
5252
https://fully-qualified-iothubname.azure-devices.net/twins/{deviceId}/methods?api-version=2021-04-12
5353
```
5454
5555
* The POST *method*
5656
57-
* *Headers* that contain the authorization, request ID, content type, and content encoding.
57+
* *Headers* that contain the authorization, content type, and content encoding.
5858
5959
* A transparent JSON *body* in the following format:
6060
6161
```json
6262
{
63+
"connectTimeoutInSeconds": 200,
6364
"methodName": "reboot",
6465
"responseTimeoutInSeconds": 200,
6566
"payload": {
@@ -75,7 +76,7 @@ The value provided as `connectTimeoutInSeconds` in the request is the amount of
7576
7677
#### Example
7778
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.
7980
8081
To begin, use the [Microsoft Azure IoT extension for Azure CLI](https://github.com/Azure/azure-iot-cli-extension) to create a SharedAccessSignature.
8182
@@ -100,14 +101,15 @@ curl -X POST \
100101
}'
101102
```
102103

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.
104105

105106
> [!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:
108+
>
109+
> ```bash
110+
> https://<iothubName>.azure-devices.net/twins/<deviceId>/modules/<moduleName>/methods?api-version=2021-04-12
111+
> ```
107112
108-
```bash
109-
https://<iothubName>.azure-devices.net/twins/<deviceId>/modules/<moduleName>/methods?api-version=2021-04-12
110-
```
111113
### Response
112114
113115
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:
117119
* 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);
118120
* 504 indicates gateway timeout caused by device not responding to a direct method call within `responseTimeoutInSeconds`.
119121
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.
121123
122124
* A JSON *body* in the following format:
123125
@@ -128,25 +130,25 @@ The back-end app receives a response that is made up of the following items:
128130
}
129131
```
130132
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.
132134
133135
### Method invocation for IoT Edge modules
134136
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.
136138
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.
138140
139141
## Handle a direct method on a device
140142
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.
142144
143145
### MQTT
144146
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).
146148
147149
#### Method invocation
148150
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.)
150152
151153
The body that the device receives is in the following format:
152154
@@ -171,7 +173,7 @@ The body is set by the device and can be any status.
171173
172174
### AMQP
173175
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).
175177
176178
#### Method invocation
177179

0 commit comments

Comments
 (0)