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-bridge

    That is all setup and working and I can use PostMan to test with this
    0_1599182132378_985357a4-8094-4d27-b0f7-1e7e6754219c-image.png

    This works fine. I get the data into Azure IoT. It looks lovely.
    0_1599182928983_fd183531-7197-424f-a455-c324d261576a-image.png

    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:
    0_1599182443317_93903347-5378-4580-978d-f0afe6a2db11-image.png

    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


  • M5Stack

    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)

    0_1599216414009_Screenshot_20200904-124349.png

    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.



  • @russell

    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.

    0_1599233508721_Screen Shot 2020-09-04 at 11.22.59 AM.png

    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.
    0_1599286949721_4da940a3-3ab8-41bb-90e4-cc0f91afd8fd-image.png

    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.
    0_1599287350659_e3922b1d-0018-4bad-9342-8c2444f1dd5a-image.png



  • 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.