Using VS Code to call a REST API in Azure IoT Hub [+ useful stuff]

Published on August 24, 2018

Using VS Code to call a REST API in Azure IoT Hub [+ useful stuff]

Original author: formulahendry
  • Transfer
  • Tutorial
Hi, Habr! Today we are sharing a mini-tutorial on using Visual Studio Code to call the REST API in the Azure internet hub. Consider all the necessary tools, creating an HTTP request, generating SAS and sending a request. Well, at the end of the article you will find 3 useful resources on the topic.



The REST API for the Center for Internet of Things (IoT Hub) provides programmatic access to device services, messages and tasks, as well as to the resource provider in the Internet of Things Center. The Azure IoT Toolkit for Visual Studio Code extension simplifies the use of the IoT Hub REST APIs in VS Code, without the need for an additional chain of tools. Let's see how easy it is to send messages from your device to the cloud using the Azure Internet of Things Center!

Required items



Creating an HTTP request


In VS Code, create the file d2c.http with the following CURL request, then replace {iothub-name} and {device-id}:

curl --request POST \
  --url 'https://{iothub-name}.azure-devices.net/devices/{device-id}/messages/events?api-version=2018-06-30' \
  --header 'authorization: {sas-token}' \
  --data 'Hello IoT Hub!'

SAS Marker Generation


Right-click on your device and select Generate SAS Token for Device . The SAS token will be created and placed on the clipboard, after which you need to replace {sas-token} d2c.http in it .



Sending an HTTP request


  1. Right-click on your device and select Start Monitoring D2C Message to initiate monitoring of messages sent from the device to the cloud.
  2. In the d2c.http file, click the Send Request link to call the Azure IoT Hub REST API to send a d2c message.
  3. In the answer area on the right, you will see an HTTP response in the form of “HTTP 204”, which means that the message was successfully sent.
  4. On the Output Channel in the Azure IoT Toolkit, you will see that the IoT Hub received the message “Hello IoT Hub!”.



You do not need an extra chain of tools to invoke the Azure IoT REST APIs in Visual Studio Code. If you want to learn more about how the Azure IoT Toolkit extension helps developers make the most of the Azure IoT Hub, visit our Wiki page for detailed descriptions and lessons.

Useful materials


[E-book] How to grow with Azure


This guide is designed to help your team more effectively implement and integrate cloud technologies. You will get to know key cloud concepts, as well as tips for working with Azure, DevOps, integration, planning, and more.

Download

Evolution of server computing: Virtual machines, containers or serverless - what to choose?


Thanks to the rapid advances in cloud computing, developers now have a number of capabilities to create and deploy applications and services. For example, new computing capabilities, such as containers and serverless, provide more flexibility in determining how your organization consumes computing resources. Although the options are excellent, they can also make it difficult to choose the right technology for each need. What to choose? Read in this study.

Download

[Mini ebook] Benefits of Azure Cosmos DB


Read the e-book Building Modern Big Data Applications Globally to learn how the ready-to-use, globally distributed Azure Cosmos DB database service is changing the way you manage data. Ensure data availability, consistency and protection, using industry-leading, industry-leading technologies for regulatory compliance and security. Start developing the best applications for your users based on one of five well-defined consistency models.

Download