Skip to content

Commit 97ddb3d

Browse files
committed
Add detailed instructions for Direct Method api
1 parent 12c3856 commit 97ddb3d

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,19 @@ The value provided as `connectTimeoutInSeconds` in the request is the amount of
7676
7777
#### Example
7878
79-
See below for a barebone example using `curl`.
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.
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.
82+
83+
```bash
84+
az iot hub generate-sas-token -n <iothubName> -du <duration>
85+
```
86+
87+
Next, replace the Authorization header with your newly generated SharedAccessSignature, then modify the `iothubName`, `deviceId`, `methodName` and `payload` parameters to match your implementation in the example `curl` command below.
8088

8189
```bash
8290
curl -X POST \
83-
https://iothubname.azure-devices.net/twins/myfirstdevice/methods?api-version=2018-06-30 \
91+
https://<iothubName>.azure-devices.net/twins/<deviceId>/methods?api-version=2018-06-30 \
8492
-H 'Authorization: SharedAccessSignature sr=iothubname.azure-devices.net&sig=x&se=x&skn=iothubowner' \
8593
-H 'Content-Type: application/json' \
8694
-d '{
@@ -93,6 +101,14 @@ curl -X POST \
93101
}'
94102
```
95103

104+
Execute the modified command to invoke the specified Direct Method. Successful requests will return an HTTP 200 status code.
105+
106+
> [!NOTE]
107+
> 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:
108+
109+
```bash
110+
https://<iothubName>.azure-devices.net/twins/<deviceId>/modules/<moduleName>/methods?api-version=2018-06
111+
```
96112
### Response
97113

98114
The back-end app receives a response that is made up of the following items:

0 commit comments

Comments
 (0)