Post sensor data from python (Response 500 error) SOLVED

Strugglig to make this work. Hosted FarmOS migrated to v2
Same output when posting to the sensor asset url with no data stream,
and when the stream is created, and posting to the stream url.

import requests

url = 'url from developer info'
myobj = { "timestamp": 1644346974, "value": 66 }

header = {'Content-type': 'application/json'}
x = requests.post(url, data=str(myobj), headers=header)
print(x)

Output:

<Response [500]>

Process finished with exit code 0
1 Like

@pat I think you need json=myobj instead of data=str(myobj).

1 Like

Yes, passing your data in via the json parameter is easiest!

This part of the requests documentation describes POSTing json with both approaches: Quickstart — Requests 2.27.1 documentation

Also, make sure the private_key parameter is included. I’m not 100% sure the parameters will work if passed in via the url parameter. Quickstart — Requests 2.27.1 documentation

2 Likes

FWIW, I checked the logs on Farmier and this is what it showed:

TypeError: Argument 2 passed to Drupal\\data_stream\\Plugin\\DataStream\\DataStreamType\\Basic::storageSave() must be of the type array, null given

@paul121 I wonder if we should open a feature request to improve the handling in that logic, and provide a proper error function + message when data is malformed?

2 Likes

You’re absolutely right. It worked.
I got response 201, and believed that was a new error…
But then I learned this : 201 means “created”

2 Likes

Yep, that is a good idea. Some additional handling around here? https://github.com/farmOS/farmOS/blob/cb5d969d027df4c99a0ed3b50a35102d1760782c/modules/core/data_stream/src/Plugin/DataStream/DataStreamType/Basic.php#L307

1 Like