Suggest on how to send device message to IoT-hub using RESTAPI

Hsieh, Todd T 91 Reputation points
2020-11-06T02:16:19.98+00:00

I would like to use REST APIs to send device message to IoT hub without using the client APIs. There is an example on how to use that for file upload:

GET (REST API)
{iot hub}.azure-devices.net/devices/{deviceId}/files/{filename}.

which will return a storage info that contains a sas token. Then we use the token to upload a file.

For sending a message securely, should there a similar REST API to return sas token and place it like this:

--url
POST
{iot hub}.azure-devices.net/
devices/{id}/messages/message?api-version=2020-03-13

--header 'authentication: {sas token}'
--data 'my device message'

I visited this link and it suggests to use x.509 and doesn't seem to require any security key etc?
https://stackoverflow.com/questions/56027387/azure-iot-hub-http-device-to-cloud-messages-without-sdk

There are some link to generate sas token locally but wonder if the IoT-hub can generate sas token instead.

Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,189 questions
0 comments No comments
{count} votes

Accepted answer
  1. Matthijs van der Veer 4,376 Reputation points MVP
    2020-11-06T06:52:08.187+00:00

    There is currently no support to have IoT Hub generate a security token. You will have to generate it locally, using the access key found in IoT Hub. This is done to avoid sending keys over the internet, as a security precaution. You can read more about it here.

    When you do calculate that key, the correct endpoint to send device messages would be

    POST https://fully-qualified-iothubname.azure-devices.net/devices/{id}/messages/events?api-version=2020-03-13  
    
    4 people found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.