Batch load via cURL and JSON file

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
tannkar
 
Posts: 2
Joined: Fri Apr 07, 2017 8:19 pm

Batch load via cURL and JSON file

Post by tannkar »

I currently have a free account and am attempting to create multiple records via cURL and a JSON file. I have attempted to do this on both a Mac and Windows system. When I run the cURL command below, I do not get any errors but it also does not provide a response.
Here is a sample of my command:

Code: Select all

curl -H "Content-Type: application/json" -d @batch.json -H "X-AIO-Key: {x-aio-key}" https://io.adafruit.com/api/v2/tannkar/feeds/sample.006/data/batch
Here is a sample of my JSON file:

Code: Select all

[{
"value": 112,
"created_at": "2019-08-01T06:00:00Z"
},
{
"value": 112.4,
"created_at": "2019-08-01T18:00:00Z"
}]
Can anyone provide some insight of what I may be doing wrong or are batch loads not allowed with a free account?

FYI:
The following syntax for a single record is accepted successfully using a Mac:

Code: Select all

curl -H "Content-Type: application/json" -d '{"value":173,"created_at":"2019-08-10T18:0000-06:00","lat":44.940591, "lon":"-102.455239"}' -H "X-AIO-Key: {x-aio-key]" https://io.adafruit.com/api/v2/tannkar/feeds/sample.392/data
The following syntax for a single record is accepted successfully using Windows:

Code: Select all

curl -H "Content-Type: application/json" -d "{\"value\":173,\"created_at\":\"2019-08-10T18:0000-06:00\",\"lat\":44.940591, \"lon\":\"-102.455239\"}" -H "X-AIO-Key: {x-aio-key]" https://io.adafruit.com/api/v2/tannkar/feeds/sample.392/data

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

Re: Batch load via cURL and JSON file

Post by jwcooper »

Have you already created these feeds and are using the proper feed key as displayed on your feeds page?

https://io.adafruit.com/tannkar/feeds

Here is further documentation that may help:
https://io.adafruit.com/api/docs/#creat ... ta-records

You can batch upload with a free account. You will just need to be mindful of the data rate limitations as part of that account.

User avatar
tannkar
 
Posts: 2
Joined: Fri Apr 07, 2017 8:19 pm

Re: Batch load via cURL and JSON file

Post by tannkar »

Yes, I have verified the feed keys. I have created the syntax for adding a single record (listed at the bottom of my first post) which was added successfully. I then modified that same line, removing the data information and substituting in the JSON filename and then adding the "/batch" to the end of the URL. Yet, when I post this command, it just comes back to the command prompt without any errors or a response from io.adafruit.com.
I wondered if it could be a JSON file issue, so I intentionally modified the JSON file with incorrect syntax and re-ran the command. When I do this it will return an error that it cannot parse the JSON file, so I believe it is not an issue with syntax in the JSON file. After this, I'm not sure what to try next. Any other suggestions would be much appreciated!
I did not want to post the actual code in my first post (slightly modified the feed_key and removed the AIO-key) but if it would help to see the actual code, please let me know.

User avatar
charles_w
 
Posts: 3
Joined: Wed Mar 28, 2018 1:58 pm

Re: Batch load via cURL and JSON file

Post by charles_w »

I just figured this out myself, the issue is your json. The expected json is an object (not array as documented) with a key "data" and value of the array.

Code: Select all

{
  "data": [
    {
      "value": 112,
      "created_at": "2019-08-01T06:00:00Z"
    },
    {
      "value": 112.4,
      "created_at": "2019-08-01T18:00:00Z"
    }
  ]
}
Hope this helps!

User avatar
kjmclark
 
Posts: 13
Joined: Fri Feb 08, 2013 2:02 pm

Re: Batch load via cURL and JSON file

Post by kjmclark »

Was there any resolution to this question? That last response is interesting, but doesn't seem like what the OP was trying to do. They weren't attempting to put multiple readings for a single feed in, they were attempting to load a single reading for multiple feeds at once. That's a pretty common case - at this time point, these were the value for these five feeds.

User avatar
kjmclark
 
Posts: 13
Joined: Fri Feb 08, 2013 2:02 pm

Re: Batch load via cURL and JSON file

Post by kjmclark »

Actually, I should be explicit, maybe someone will show a direct answer. So, I have a group "group_1" with key "group-1", and a bunch of feeds in it:
group_1
temp_1
temp_2
voltage_1
voltage_2

So, the group key is group-1, the feeds are temp-1, temp-2, voltage-1, voltage-2.

I want to send a single set of readings for all of them in one post. They're all for the same time point - basically the time I'm doing the post is fine. You might think the curl command for that would be like:
curl -X POST https://io.adafruit.com/api/v2/myname/g ... /data.json
-H "X-AIO-Key: aio_abcDEF120BCDad987adDA1221678"
-H "Content-Type: application/json"
-d '{\"temp-1\": 23, \"temp-2\":28, \"voltage-1\":12.5, \"voltage-2\":12.6}'

(No, that's not my real aio key.) That doesn't work. It says you need feeds. OK, but where? On a whim, I stuck a feeds section in the json:
-d '{\"feeds\": {\"temp-1\": 23, \"temp-2\":28, \"voltage-1\":12.5, \"voltage-2\":12.6} }'

which doesn't work either. Now it says OK, status 200, but nothing happened. No data were added for my feeds.

User avatar
kjmclark
 
Posts: 13
Joined: Fri Feb 08, 2013 2:02 pm

Re: Batch load via cURL and JSON file

Post by kjmclark »

Here we go. There's a verbose way to do it that works. The JSON looks like this:
{
"feeds": [
{
"key": "temp_1",
"value": 72
},
{
"key": "temp_2",
"value": 74
},
{
"key": "voltage_1",
"value": 12.1
}, {
"key": "voltage_2",
"value": 9.1
}
]
}

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”