HTTP POST to Azure IoT
-
Hey,
Im relatively new to all this - and I've got a fair way but just can't replicate this POST command to get data into the Azure IoT platform.
Ive successfully setup a Azure IOT Central App that can receive HTTP Post and then record the measurements
See - https://github.com/Azure/iotc-device-bridgeThat is all setup and working and I can use PostMan to test with this
This works fine. I get the data into Azure IoT. It looks lovely.
I just want to replicate this simple HTTP Post with JSON data payload in a UIFlow. I can then use my m5Stickc to send the real data.
Here is my attempt:
I have tried many variations of the 'data' payload - sometimes I get 400 errors - sometimes I get responses to say the 'device' is not correctly specified - but most of my attempts I get no response back.
What would be the best way to recreate the POST command in UIFlow?
Im - close - so thanks anybody that can help :)
Russell
-
not idea. maybe you could check the request header does same to the postman test sample
-
In simple terms without all the story, how do you do a HTML Post with this data?
{
"device": {
"deviceId": "postmantest"
},
"measurements": {
"Temp": 29.31,
"Pressure": 44,
"humidity": 8.3,
"ledColor": "red"
}
}For those who do code - I dont - postman says the code would be..
curl --location --request POST 'https://[sitecode].azurewebsites.net/api/IoTCIntegration?code=[key]'
--header 'Content-Type: text/plain'
--data-raw '{
"device": {
"deviceId": "postmantest"
},
"measurements": {
"Temp": 29.31,
"Pressure": 44,
"humidity": 8.3,
"ledColor": "red"
}
}' -
You could try a create manual string in json format something like that (incomplete example)
and send this rawdata variable, maybe this work, ewentually try change content type header from text to json. or send your DataMap without dump json block.
-
I think your DataMap and JSONdata payload looks good and is formatted correctly. Here is a slightly modified flow that has a few tweaks to set the content-type to JSON (since you are posting JSON data) and to remove the "get data" from the success/fail on HTTP request. I don't think you need that since you are doing HTTP POST and not GET. Plus you are already printing the JSONdata to the screen just before the HTTP Request.
Since I'm not an Azure IoT user, I used an Adafruit IO Webhook to verify the flow operates correctly to get data to the cloud. I get a status code 200 (HTTP OK) with the flow above. I also had to add a header with my AIO secret key, so you might need to do the same with the Azure "code" secret key. Please verify that with the Azure IoT API documentation. Give it a try and see how it goes. If it gives something other than HTTP 200 for the status, let us know what the error code is so we can troubleshoot further.
Also, I have noticed before if I try to do too much logic in the success/fail sections of the HTTP Request block, strange things can happen. So I like to keep it simple and just capture the status code to a variable and then print it after the HTTP Request has finished.
-
@robalstona Yes - I did go down this route and it did work btw - Thanks
-
@world101 This also worked! Thanks.
The 'Content-Type' was the key step it looks like.
I had to put in those 'waits' else I randomly got 'Local variable referenced before assignment' errors during the POST step - which I can only guess at not having a Status code to set for status and then trying to Print that status. Its still not very reliable - It often hangs at that HTTP step and I have to reset.
Anyway - progress! Here is the nice little graph of 'realworld' data from the m5stick in a dashboard in Azure IoT - thanks for your help.
-
Looks good 👍🏻👍🏻
-
I notice that http request function take some time to receive response (approx. 1-3 seconds) dependent of service you used. Requests function in python is rather blocking function, and rest of program should wait for end of this function work.
-
Just wanted to thank you for posting this. It helped me a great deal.