Please note: Friday June 18 is a holiday celebrating Juneteenth, please allow extra time for your order to arrive and plan accordingly.
0

Trouble posting JSON data
Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Please be positive and constructive with your questions and comments.

Trouble posting JSON data

by Foamyguy on Sat May 22, 2021 1:04 pm

I am attempting to follow the instructions from the cookbook here:

aio_json_docs.png
aio_json_docs.png (113.26 KiB) Viewed 305 times


https://io.adafruit.com/api/docs/cookbo ... on-strings

The POST does complete successfully, I get a 200 status response. But the resulting feed value does not contain all of the data. It only contains a single one of the keys from the data, not any of the values, and not the other key either.

Here is a screenshot of the resulting feed value:

aio_feed_value.png
aio_feed_value.png (8.59 KiB) Viewed 305 times


I am using python requests to post my data:

Code: Select all | TOGGLE FULL SIZE
fake_data = {
  "value": {"sensor-1":22.587,"sensor-2":13.182},
  "lat": 38.1123,
  "lon": -91.2325,
  "ele": 112
}
resp = requests.post(aio_url, data=fake_data, headers=headers)
print(resp.status_code)

Foamyguy
 
Posts: 47
Joined: Mon May 26, 2014 4:24 pm

Re: Trouble posting JSON data

by jwcooper on Mon May 24, 2021 10:36 am

Looks like we may need to update that specific cookbook. It's a bit misleading in that it's teaching about properly encoding JSON for IO, but not using the right endpoints.

If you want to send multiple data endpoints, you can do it via the group or batch functionality:
https://io.adafruit.com/api/docs/#creat ... ta-records
https://io.adafruit.com/api/docs/#create-group-data

Also, if you are trying to save data via IO as JSON itself, the above examples you linked should work, but the data won't be usable within IO, such as in dashboards. Dashboards need the actual data to be saved to each feed to be usable, and not json representations of multiple feed values.

If you try to pull the data down via the api for that feed, are you still seeing only the sensor-2 value?

jwcooper
 
Posts: 784
Joined: Tue May 01, 2012 9:08 pm

Re: Trouble posting JSON data

by Foamyguy on Sat May 29, 2021 12:43 pm

I fetched the data by sending GET request (with AIO key in header) to this URL:

https://io.adafruit.com/api/v2/Foamyguy ... test-post/

In the data that I got it does only have "sensor-2" in the last_value field:

Code: Select all | TOGGLE FULL SIZE
{
...
'last_value': 'sensor-2',
...
}


In the end I was able to get it working by escaping my multiple data values into a string inside the value filed like this:

Code: Select all | TOGGLE FULL SIZE
fake_data = {
    "value": json.dumps({"sensor-1": 22.587, "sensor-2": 13.182}),
    "lat": 38.1123,
    "lon": -91.2325,
    "ele": 112
}

Foamyguy
 
Posts: 47
Joined: Mon May 26, 2014 4:24 pm

Please be positive and constructive with your questions and comments.