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.
Locked
User avatar
Foamyguy
 
Posts: 65
Joined: Mon May 26, 2014 4:24 pm

Trouble posting JSON data

Post by Foamyguy »

I am attempting to follow the instructions from the cookbook here:
aio_json_docs.png
aio_json_docs.png (113.26 KiB) Viewed 443 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 443 times
I am using python requests to post my data:

Code: Select all

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)

User avatar
jwcooper
 
Posts: 1004
Joined: Tue May 01, 2012 9:08 pm

Re: Trouble posting JSON data

Post by jwcooper »

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?

User avatar
Foamyguy
 
Posts: 65
Joined: Mon May 26, 2014 4:24 pm

Re: Trouble posting JSON data

Post by Foamyguy »

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

{
...
'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

fake_data = {
    "value": json.dumps({"sensor-1": 22.587, "sensor-2": 13.182}),
    "lat": 38.1123,
    "lon": -91.2325,
    "ele": 112
}

Locked
Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.

Return to “Internet of Things: Adafruit IO and Wippersnapper”