Reputation: 733
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:
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.
Upvotes: 1
Views: 2179
Reputation: 800
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
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
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
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'
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.
Happy Azure IoT Learning
Upvotes: 5