Send payload to my web application using http request

Hi everyone, I have retrieved msg.payload from my IOT sensors, and I wonder how I can send msg.payload to my flask web application using http request or other method.

Thank you.

Welcome to the forum. However, you've not really given us anything to go on. We have no context about your web application so can't really help.

Sorry about that, I have upload a screenshot of my flask application.

You can send the request node POSTing data to your server:

grafik

Example flow:

grafik

[{"id":"12fde2238ad56ee2","type":"http request","z":"94c514c2ac360b08","name":"","method":"POST","ret":"txt","paytoqs":"ignore","url":"http://127.0.0.1:1880/receive_power","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":370,"y":180,"wires":[["a09478c1b756579b"]]},{"id":"e442c220d9d3430d","type":"http in","z":"94c514c2ac360b08","name":"","url":"/receive_power","method":"post","upload":false,"swaggerDoc":"","x":220,"y":320,"wires":[["f53b16c3af14be08","0d1f3e05f092db78"]]},{"id":"98db9a79c6719e6a","type":"inject","z":"94c514c2ac360b08","name":"sensor data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"maschine_id\":\"1\",\"machine_power\":\"200\"}","payloadType":"json","x":190,"y":180,"wires":[["12fde2238ad56ee2"]]},{"id":"a09478c1b756579b","type":"debug","z":"94c514c2ac360b08","name":"debug 15","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":540,"y":180,"wires":[]},{"id":"f53b16c3af14be08","type":"http response","z":"94c514c2ac360b08","name":"","statusCode":"","headers":{},"x":490,"y":320,"wires":[]},{"id":"0d1f3e05f092db78","type":"debug","z":"94c514c2ac360b08","name":"debug 16","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":480,"y":360,"wires":[]},{"id":"f576842b9cde1189","type":"comment","z":"94c514c2ac360b08","name":"send post request","info":"","x":190,"y":140,"wires":[]},{"id":"575b9c1094810d37","type":"comment","z":"94c514c2ac360b08","name":"dummy server","info":"","x":190,"y":280,"wires":[]}]

you can read more about the request node here: https://cookbook.nodered.org/#http-requests

This is my setup.


I think in this case what I doing this POSTing the data from my web server to node red, which I should be doing the other way around.

Whats is the payload? The server send something back, but the http request node can't convert it to json.

You can set the request that it return a string:
grafik

can you see that the data ist ingested into your backend?

And your backend use the Object requests.form.machine_id for extracteing the data, so try sending this payload to the backend:

{
    "payload": {
        "form": {
            "maschine_id": "1",
            "machine_power": "200"
        }
    }
}

you can use the change node:

[{"id":"98db9a79c6719e6a","type":"inject","z":"94c514c2ac360b08","name":"sensor data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"maschine_id\":\"1\",\"machine_power\":\"200\"}","payloadType":"json","x":190,"y":180,"wires":[["751195e907674896"]]},{"id":"751195e907674896","type":"change","z":"94c514c2ac360b08","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.form","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":410,"y":180,"wires":[["12fde2238ad56ee2","c6d2425f68837d2b"]]}]

Thank you for your reply.

1. My msg.object is this:

{"topic":"servicelocation/1df4d811-cd15-44db-b358-1caa61cf055d/realtime","payload":{"totalPower":0,"totalReactivePower":9,"totalExportEnergy":2271,"totalImportEnergy":8426,"monitorStatus":0,"utcTimeStamp":1713851721210,"channelPowers":[{"ctInput":0,"power":0,"exportEnergy":2271,"importEnergy":8426,"phaseId":0,"current":0}],"voltages":[{"voltage":235,"phaseId":0}]},"qos":0,"retain":false,"_msgid":"e5ca5b187342aa1b"}

2. In my backend, I get error message like this:

192.168.137.116 - - [23/Apr/2024 18:22:40] "POST /receive_power HTTP/1.1" 500 -
Traceback (most recent call last):
File "C:\Users\shawn\OneDrive\Desktop\Unimelb-2024-SEM-1\old_code.venv\Lib\site-packages\flask\app.py", line 1488, in call
return self.wsgi_app(environ, start_response)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\shawn\OneDrive\Desktop\Unimelb-2024-SEM-1\old_code.venv\Lib\site-packages\flask\app.py", line 1466, in wsgi_app
response = self.handle_exception(e)
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\shawn\OneDrive\Desktop\Unimelb-2024-SEM-1\old_code.venv\Lib\site-packages\flask\app.py", line 1463, in wsgi_app
response = self.full_dispatch_request()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\shawn\OneDrive\Desktop\Unimelb-2024-SEM-1\old_code.venv\Lib\site-packages\flask\app.py", line 872, in full_dispatch_request
rv = self.handle_user_exception(e)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\shawn\OneDrive\Desktop\Unimelb-2024-SEM-1\old_code.venv\Lib\site-packages\flask\app.py", line 870, in full_dispatch_request
rv = self.dispatch_request()
^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\shawn\OneDrive\Desktop\Unimelb-2024-SEM-1\old_code.venv\Lib\site-packages\flask\app.py", line 855, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\Users\shawn\OneDrive\Desktop\Unimelb-2024-SEM-1\old_code\package\code\server\server_app.py", line 1296, in receive_power
machine_list.get(machine_id).power_generate=int(machine_power)
^^^^^^^^^^^^^^^^^^
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'NoneType'

3. In the debug panel in Node-Red, when I set the request that it returns a string, I was seeing the error message like above sending from my backend.

4. This is a modification of my code before all of the above happened.

5. I was not using send_power_test.py in this scenario, I was just trying to send the JSON object to this url: /receive_power.

Maybe your dic object is empty?

in this tutorial, you get the dic with the obj.json property. But im not a flask expert :smiley:
https://sentry.io/answers/flask-getting-post-data/#json-data

from flask import Flask, request
# ...

@app.route('/json_example', methods=['POST'])
def handle_json():
    data = request.json
    print(data.get('name'))
    print(data.get('age'))
    return data

Thanks for your help kitori. You Node-red flow is of great help.

I have solved my problems. My orginial code is for receiving form data, not json object.

Have a good day. :grinning:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.