1
votes

I am trying to learn the Azure IoT hub, but I am unable to send data to my IoT hub using Postman. I did the following:

  • Create Azure IoT hub
  • Create a device using Azure Portal CLI
  • Copied the text where it said "primary key" on the Device page

My request looks as follows:

POST https://myhub.azure-devices.net/devices/MyDevice/messages/events?api-version=2020-03-13

I have added the authorization header, which has the value: SharedAccessSignature=SharedAccessSignature sr=myhub.azure-devices.net%2Fdevices%2FMyDevice&sig=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Where the key is the copied text from the "primary key" field from the device page.

Content-type is set to application/json. But the body is currently empty.

I am getting the following response:

"Message": "ErrorCode:ServerError;InternalServerError",

Note: Hub and device name are not the same as stated here.

2

2 Answers

3
votes

For a quick-start on this, we have 3 stages 1) SAS key generation from Visual Studio Code see below images(Or from CLI please see the answer posted by silent) 2) Use Postman 3) Monitor via VS code

Steps: VS Code

1) Install VS Code extension tools for Azure IoT & Connect your IoT Hub as per onscreen instructions

enter image description here enter image description here

2) You can see the list of devices in VS code Explorer. Now right click the device (created in the portal) and select 'Generate SAS Token for the device', give the time in hours like 24 and hit enter.Copy the SAS key from output window fully (entire). Sample shown below.

SharedAccessSignature sr=deviothub.azure-devices.net%2Fdevices%2FNewDevice&sig=H3II%2FCa5Km5V2pieP5Ti1SI%3D&se=1590867649

enter image description here

enter image description here

Steps Postman

1) I hope you have already visited this document on Azure IoT Hub REST API Replace the required sections with your IoT Hub values.

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

2) Make sure to include the fully copied SAS token and use it in the header section of POST call

enter image description here

3) Body content : It depends on your use case. example 'raw'--JSON--> {"Temperature":54}

**You can see the accepted response from IoT Hub as '204 No Content'

Step Monitor the Telemetry the easy way

1) Open the VS Code editor and right click the particular device and slect 'Start Monitoring the in built events'

enter image description here

2) Send another telemetry from Postman and you can see the message arrived at IoT Hub and is visible on the VS code Monitoring screen.

enter image description here

Happy Azure IoT Learning

2
votes

You need to calculate the auth header based on your primary key, not just put it into the header. See here for examples how to do this, for instance by using CLI az iot hub generate-sas-token