API add_devicegroup - "We couldn't parse the provided json. Syntax error"

Hello! I’m trying to add a device group using an example from LibreNMS Docs https://docs.librenms.org/API/DeviceGroups/#get_devicegroups, but I’m getting an error - We couldn’t parse the provided json. Syntax error

curl -H 'X-Auth-Token: ***********************' \
  -d '{"name": "New Device Group", \
       "desc": "A very fancy dynamic group", \
       "type": "dynamic",
       "rules": "{\"condition\":\"AND\",\"rules\":[{\"id\":\"access_points.name\",\"field\":\"access_points.name\",\"type\":\"string\",\"input\":\"text\",\"operator\":\"equal\",\"value\":\"accesspoint1\"}],\"valid\":true}"}' \
  http://10.255.255.20/api/v0/devicegroups
{
    "status": "error",
    "message": "We couldn't parse the provided json. Syntax error"
}%

Does anyone knows what is the issue here?

Thank you

Dmitri

Never done it, but it got me curious … I think the example in the doco is wrong for two reasons: it’s missing -X POST in the dynamic example, and the newline breaking in the middle of the json doesn’t work.

If you add -X POST and put the json string on one continuous line it works:

curl -X POST -H "X-Auth-Token: $LNMSAPI" "${APIPATH}devicegroups" \
-d \
'{"name": "New Device Group", "desc": "A very fancy dynamic group", "type": "dynamic", "rules": "{\"condition\":\"AND\",\"rules\":[{\"id\":\"access_points.name\",\"field\":\"access_points.name\",\"type\":\"string\",\"input\":\"text\",\"operator\":\"equal\",\"value\":\"accesspoint1\"}],\"valid\":true}"}'

If you want to break it up for readability, the best example of how to do this I’ve seen rather that complicated escaping or heredocs is this:

curl  -X POST -H "X-Auth-Token: $LNMSAPI" "${APIPATH}devicegroups" \
--data-raw '
{
 "name": "New Device Group", 
 "desc": "A very fancy dynamic group",
 "type": "dynamic", 
 "rules": "{\"condition\":\"AND\",\"rules\":[{\"id\":\"access_points.name\",\"field\":\"access_points.name\",\"type\":\"string\",\"input\":\"text\",\"operator\":\"equal\",\"value\":\"accesspoint1\"}],\"valid\":true}"
}
'

Do they work?

Both solutions worked well.

Solution #1

curl -X POST -H "X-Auth-Token: ********************" \
-d \
'{"name": "New Device Group", "desc": "A very fancy dynamic group", "type": "dynamic", "rules": "{\"condition\":\"AND\",\"rules\":[{\"id\":\"access_points.name\",\"field\":\"access_points.name\",\"type\":\"string\",\"input\":\"text\",\"operator\":\"equal\",\"value\":\"accesspoint1\"}],\"valid\":true}"}' \
http://10.255.255.20/api/v0/devicegroups
{
    "status": "ok",
    "id": 16,
    "message": "Device group New Device Group created"
}%                            

Solution #2

% curl  -X POST -H "X-Auth-Token: ********************" \
--data-raw '
{
 "name": "New Device Group",
 "desc": "A very fancy dynamic group",
 "type": "dynamic",
 "rules": "{\"condition\":\"AND\",\"rules\":[{\"id\":\"access_points.name\",\"field\":\"access_points.name\",\"type\":\"string\",\"input\":\"text\",\"operator\":\"equal\",\"value\":\"accesspoint1\"}],\"valid\":true}"
}
' \
http://10.255.255.20/api/v0/devicegroups
{
    "status": "ok",
    "id": 17,
    "message": "Device group New Device Group created"
}%                     

Excellent, I updated the doco with the working examples and submitted a PR: